Is it me ????? compiler float issue!!!

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
richardb
Posts: 310
Joined: Tue Oct 03, 2006 8:54 pm

Is it me ????? compiler float issue!!!

Post by richardb » Thu Nov 19, 2009 11:58 am

i'm just making some test kit and using the pic to check supply rails. it seems that comparing negative floats just doesnt work.

am i doing something stupid or is this a compiler bug??

Code: Select all



Device = 18F1320
Clock = 40

Config OSC = HSPLL
#option USART_BRGH = false
#option MyDebug = true

Include"SUART.bas"'FOR DEBUG
Include"Float_conv.bas"
Include "utils.bas"                                                                                                                         
Include "ADC.bas"

Dim LED1 As PORTB.0
dim LED2 as portb.1
Dim Voltage As Float
Const Quanta = 11*(5/1024.0)  '(gives voltage *100)
'------SUBS START------------------------------------------ 
Function myfloattostr(f As Float,n As Byte) As String (8)
    FloatToStrSE(f,result,n)
End Function
'-----start-----------------------------------------------
    SetAllDigital
    TRISA = %11111111
    TRISB = %01100000 ' see I/O spreadsheet
    ADCON1 = %11100000'ADCON1.7 = 1             // set analogue input on PORTA.0
    ADC.SetConvTime(FOSC_64)                                   
    ADC.SetAcqTime(20)
    UART.SetTX(PORTB.7)'debug
    UART.SetBaudrate(sbr57600)
    UART.SetMode(umInverted)
'-----main-----------------------------------------------------
While TRUE
    DelayMS(500) 
    Voltage = 5- ((1023-ADC.Read(0))*Quanta)  ' measured relative to +5v rail '11*(5/1024.0)
    UART.Write(13,10," {{{{",myfloattostr(Voltage,2),"}}}}")
    If Voltage < -10.0  Then     
        UART.Write(" true ")
        LED1 = 1
    Else
        UART.Write(" false ")
        led1 = 0
    EndIf
    
    If integer(Voltage) < -10  Then     
        UART.Write(" true ")
        led2=1
    Else
        UART.Write(" false ")
        led2=0
    EndIf
    UART.Write(13,10)
Wend 
End

I'm putting a 0-5v signal into the pic input which represents 0 to -55 V

if i put 12.4 volts in the terminal reads
"{{{{-12.40}}}} true true "

if i put -8.5 v in it reads the following

" {{{{-8.53}}}} true false"

any suggestions????

Richard
Hmmm..

richardb
Posts: 310
Joined: Tue Oct 03, 2006 8:54 pm

Post by richardb » Thu Nov 19, 2009 2:08 pm

i thought id try this on another pic and remove the need for the adc for testing.

Code: Select all

Device = 18F458
Clock = 20

Config OSC = HS
#option USART_BRGH = false
#option MyDebug = true

Include"SUART.bas"'FOR DEBUG
include"convert.bas"
Include "utils.bas"                                                                                                                         


Dim LED1 As PORTB.0
Dim LED2 As PORTB.1
Dim Voltage As Float
Const Quanta = 11*(5/1024.0)  '(gives voltage *100)
'------SUBS START------------------------------------------ 

'-----start-----------------------------------------------
    SetAllDigital
   
    TRISB = %01100000 ' see I/O spreadsheet

    UART.SetTX(PORTB.7)'debug
    UART.SetBaudrate(sbr57600)
    UART.SetMode(umInverted)
    Voltage = -13
'-----main-----------------------------------------------------

While TRUE
    If Integer(Voltage) > -9 Then
        Voltage = -13 
    Else
        Voltage = Voltage + 0.1 
    EndIf
    DelayMS(1000) 
   
    UART.Write("Voltage= ",floattostr(Voltage,2)," ")
    If Voltage < -10.0  Then     
        Write(" true ")
        LED1 = 1
    Else
        Write(" false ")
        LED1 = 0
    EndIf
    
    If Integer(Voltage) < -10  Then     
        Write(" true ")
        LED2=1
    Else
        Write(" false ")
        LED2=0
    EndIf
    UART.Write(13,10)
Wend 
End
   	
heres the output
Voltage= -12.89 true true

Voltage= -12.79 true true

Voltage= -12.69 true true

Voltage= -12.59 true true

Voltage= -12.49 true true

Voltage= -12.39 true true

Voltage= -12.29 true true

Voltage= -12.19 true true

Voltage= -12.09 true true

Voltage= -11.99 true true

Voltage= -11.89 true true

Voltage= -11.79 true true

Voltage= -11.69 true true

Voltage= -11.59 true true

Voltage= -11.49 true true

Voltage= -11.39 true true

Voltage= -11.29 true true

Voltage= -11.19 true true

Voltage= -11.09 true true

Voltage= -10.99 true false

Voltage= -10.89 true false

Voltage= -10.79 true false

Voltage= -10.69 true false

Voltage= -10.59 true false

Voltage= -10.49 true false

Voltage= -10.39 true false

Voltage= -10.29 true false

Voltage= -10.19 true false

Voltage= -10.09 true false

Voltage= -9.99 true false

Voltage= -9.89 true false

Voltage= -9.79 true false

Voltage= -9.69 true false

Voltage= -9.59 true false

Voltage= -9.49 true false

Voltage= -9.39 true false

Voltage= -9.29 true false

Voltage= -9.19 true false

Voltage= -9.09 true false

Voltage= -8.99 true false
Hmmm..

User avatar
JWinters
Posts: 106
Joined: Mon Feb 04, 2008 4:56 pm
Location: North Carolina, USA
Contact:

Post by JWinters » Thu Nov 19, 2009 7:02 pm

I remember this happening to me. I couldn't compare floating point numbers in conditional statements and get them to work correctly. I ended up just using a WORD instead and converting the number later.

richardb
Posts: 310
Joined: Tue Oct 03, 2006 8:54 pm

Post by richardb » Fri Nov 20, 2009 10:17 am

i think its only an issue with negative numbers...
Hmmm..

Post Reply