View previous topic :: View next topic |
Author |
Message |
ianevans
Joined: 02 Jul 2016 Posts: 4
|
Posted: Fri Feb 09, 2018 10:09 am Post subject: Checkfloat function not working? |
|
|
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 |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Feb 09, 2018 3:01 pm Post subject: |
|
|
at first glance it appears to be a bug. i will check it later in the lib. _________________ Mark |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Feb 12, 2018 3:51 pm Post subject: |
|
|
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 |
|
|
ianevans
Joined: 02 Jul 2016 Posts: 4
|
Posted: Tue Feb 13, 2018 9:09 am Post subject: |
|
|
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 |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Feb 13, 2018 10:51 am Post subject: |
|
|
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 |
|
|
ianevans
Joined: 02 Jul 2016 Posts: 4
|
Posted: Tue Feb 13, 2018 12:13 pm Post subject: |
|
|
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 |
|
|
|