Sin and Cos problem

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

Sin and Cos problem

Post by Mast » Sun Feb 01, 2009 4:25 pm

hello,
i don't andestand the result at this :
on my windows calculator the result is

cos(31) = 0.8571
sin(31) = 0.5150

with SF
cos(31) = 0.9147
sin(31) = -0.4040


Code: Select all

Device = 18F452
Clock = 20


#option GLCD_SCREEN_WIDTH = 128  // Screen Width in Pixels
#option GLCD_SCREEN_HEIGHT = 64  // Screen Height in Pixels

#option GLCD_CS_COUNT = 2    // Count of CS Lines. Must be 2 or 3


// default module options - user options can override these values...
#option GLCD_DATA = PORTD        // data port
#option GLCD_RS = PORTB.2        // RS pin
#option GLCD_EN = PORTB.4        // EN pin
#option GLCD_RW = PORTB.3        // RW pin
#option GLCD_CS1 = PORTB.1       // chip select
#option GLCD_CS2 = PORTB.0       // chip select
#option GLCD_ASPECT_RATIO = 75   // aspect ratio, smaller number will squeeze y for GLCD circles and box
#option GLCD_INIT_DELAY = 100    // initialisation delay (ms)
#option GLCD_INVERT_CS = true   // invert CS lines... 
#option GLCD_RST = PORTB.5


Include "GLCD.bas"
Include "FixedFont.bas"
Include "ADC.bas"
Include "convert.bas"
Include "math"
////////////////////////////
Dim  ADVal, result As Word
Dim angle, X, Y, puissance As Float 
Dim text, X_text, Y_text As String
////////////////////////////

puissance = 31
////////////////////////////

    GLCD.Cls 

 X = cos(puissance)


 Y = sin(puissance)
        
            DelayMS(1000)           

            text = floattostr(puissance)
           
            X_text = floattostr(X,5)
            Y_text = floattostr(Y,5)

GLCD.SetFont(Fixed)            
GLCD.WriteAt (80,15, text)
GLCD.WriteAt (80,25, X_text)

GLCD.WriteAt (80,35, Y_text)

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Feb 01, 2009 4:53 pm

I think that the sin and cos functions are expecting an angle in radians, not degrees. To convert from degrees to radians, multiply your angle by 2Pi/360 (or approx 0.0174533).

Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

Post by Mast » Sun Feb 01, 2009 5:20 pm

Thank you Steven
Steven wrote:I think that the sin and cos functions are expecting an angle in radians, not degrees. To convert from degrees to radians, multiply your angle by 2Pi/360 (or approx 0.0174533).

Post Reply