Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Checkfloat function not working?

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
ianevans

Bascom Member



Joined: 02 Jul 2016
Posts: 4

uk.gif
PostPosted: Fri Feb 09, 2018 10:09 am    Post subject: Checkfloat function not working? Reply with quote

Hi all. I'm having trouble getting the Checkfloat function to work as described in the BASCOM-AVR (V 2.0.8.1) help.

Performing Checkfloat on Single variables that are infinity or Not A Number, do not give me the correct output.

Here is my test code:

Code:

$regfile    = "m328pdef.dat"              'ATMEGA 328P
$crystal    = 3686400                     '3.6864 MHz
$hwstack    = 64
$swstack    = 64
$framesize  = 64
$baud       = 19200

Dim s As Single , s2 As Single , s3 As Single
Dim bcheck as Byte

Print : Print "CHECKFLOAT TEST"

s2 = 1.0: s3 = 0.0 : s = s2 / s3              ' 1/0 should result in infinty
bcheck = Checkfloat(s)
Print "s = infinity, Checkfloat(s) = &B"; : Print Bin(bcheck); : Print " (expected &B00000001)"

s = 0.0
bcheck = Checkfloat(s)
Print "s = 0.0,      Checkfloat(s) = &B"; : Print Bin(bcheck); : Print " (expected &B00000010)"

s2 = -1.0 : s = sqr(s2)                     ' should result in Not A Number
bcheck = Checkfloat(s)
Print "s = NAN,      Checkfloat(s) = &B"; : Print Bin(bcheck); : Print " (expected &B00000100)"

s = -1.0
bcheck = Checkfloat(s)
Print "s = -1.0,     Checkfloat(s) = &B"; : Print Bin(bcheck); : Print " (expected &B10000000)"

End
 


The output I get is this:
Code:

CHECKFLOAT TEST
s = infinity, Checkfloat(s) = &B00000010 (expected &B00000001)
s = 0.0,      Checkfloat(s) = &B00000010 (expected &B00000010)
s = NAN,      Checkfloat(s) = &B00000010 (expected &B00000100)
s = -1.0,     Checkfloat(s) = &B10000000 (expected &B10000000)
 


Can anyone tell me what I'm doing wrong? Best Regards, Ian.

(BASCOM-AVR version : 2.0.8.1 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Fri Feb 09, 2018 3:01 pm    Post subject: Reply with quote

at first glance it appears to be a bug. i will check it later in the lib.
_________________
Mark
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Mon Feb 12, 2018 3:51 pm    Post subject: Reply with quote

The function works ok. But the single returns a 0 when you divide by 0. Likewise, the srq of a negative single returns 0.
_________________
Mark
Back to top
View user's profile Visit poster's website
ianevans

Bascom Member



Joined: 02 Jul 2016
Posts: 4

uk.gif
PostPosted: Tue Feb 13, 2018 9:09 am    Post subject: Reply with quote

Thank you for looking into this, Mark.

I'm still confused. Checkfloat does not seem to work as described in the example on avrhelp.mcselec.com.
Am I doing something wrong? Is the help out of date? Is it even possible to detect divide by zero of Single variables, for example?

Best Regards, Ian
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Tue Feb 13, 2018 10:51 am    Post subject: Reply with quote

checkfloat works ok for both doubles and singles.

But for singles there is a note : the function itself works perfect. but the test was done with fixed values. The reason is what i wrote earlier : when you divide a number by 0 in bascom, the result is 0.
That is so for integers and also for singles. It should return INF but for historical reason it returns 0. So when you test the result of divide by 0 the check works, but not as you expect.
Now it could be changed to return INF but that can break existing code.

_________________
Mark
Back to top
View user's profile Visit poster's website
ianevans

Bascom Member



Joined: 02 Jul 2016
Posts: 4

uk.gif
PostPosted: Tue Feb 13, 2018 12:13 pm    Post subject: Reply with quote

Thank you. Now I understand the results of my tests.

May I take this opportunity to say what a great tool BASCOM is for developing embedded systems!

Best Regards,
Ian
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum