[Implemented] "Special" floating point values

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

[Implemented] "Special" floating point values

Post by tinman »

Can't find whether this can already be achieved or whether has been requested, but I know there's a lot of stuff "floating" around this forum. Haha!

The ability to use, set and check for the special floating point values (NaN, +Inf, -Inf, etc) would be great.

Perhaps usable something like this:

Code: Select all

DefType.f blah
blah = #NaN
; ...
If blah = #NaN
  MessageRequester("Error", "You did not enter a valid number")
EndIf
I know the values for +/-Inf can be calculated by dividing by zero, but it doesn't seem possible to make use of such a capability. At least not in PB's native code. I've not check out the F64 lib.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Andras
User
User
Posts: 34
Joined: Wed Oct 27, 2004 6:58 am
Location: Germany

Post by Andras »

Take a look at this, you could write your own Check-Functions:

Code: Select all

QNAN_Pos.f ;Quiet NAN (Positive) 
QNAN_Neg.f ;Quiet NAN (Negative) 
SNAN_Pos.f ;Signalling NAN (Positive) 
SNAN_Neg.f ;Signalling NAN (Negative) 
INF_Pos.f ;Positiv Infinity 
INF_Neg.f ;Negative Infinity 
Zero_Pos.f ;Positive Zero 
zero_Neg.f ;Negative Zero 
IND_Neg.f ; ?

PokeL(@QNAN_Pos,%01111111111111111111111111111111) 
PokeL(@QNAN_Neg,%11111111111111111111111111111111) 
PokeL(@SNAN_Pos,%01111111101111111111111111111111) 
PokeL(@SNAN_Neg,%11111111100100010001001010101010) 
PokeL(@INF_Pos,%01111111100000000000000000000000) 
PokeL(@INF_Neg,%11111111100000000000000000000000) 
PokeL(@Zero_Pos,%10000000000000000000000000000000) 
PokeL(@zero_Neg,%00000000000000000000000000000000) 
PokeL(@IND_Neg,%11111111110000000000000000000000)

Debug QNAN_Pos 
Debug QNAN_Neg 
Debug SNAN_Pos 
Debug SNAN_Neg 
Debug INF_Pos 
Debug INF_Neg 
Debug Zero_Pos 
Debug zero_Neg 
Debug IND_Neg

If QNAN_Pos=INF_Pos 
  Debug "QNAN_POS=INF_POS?!?" 
EndIf

Somehow SNAN is debugged as QNAN, maybe I did something wrong... :-\

Also the checking for equality does not work (within PureBasic?)... So you should write special functions that directly read the bits and test, if they are in the "Infinity-Area" etc... Read IEEE 754 to find out, what areas desribe which special values...

http://en.wikipedia.org/wiki/IEEE_float ... t_standard

:!: Edited: Zero_Pos = Zero_Neg; Zero_Neg = Zero_Pos
Last edited by Andras on Sat Feb 05, 2005 12:20 am, edited 2 times in total.
Undank ist der Welten Lohn
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Andras wrote:Take a look at this, you could write your own Check-Functions:
I could, but I won't. I don't need it for my PB projects, but I used it at work in a project in VB quite successfully last week. It's more a "useful" than "can I have".
Also the checking for equality does not work (within PureBasic?)...
Testing for equality to the special floating point values should always work*. They are one specific value, and should not be any other. The problem with testing for equality with floating point numbers is apparent in any language, and while possible to equate, shouldn't normally be used since rounding errors might prevent the comparison from succeeding.

For example:

Code: Select all

a.f=0.25
If a=0.25
    Debug "this might work, dunno haven't tested"
EndIf

For a.f=0 To 0.5 Step 0.01
    If a=0.25
        Debug "this might not"
    EndIf
Next
The first might work because the numbers shoud be easily representable by binary floating point values and no calculations are involved, whereas the loop might not due to rounding errors in the incrementing of the a variable.
you should write special functions that directly read the bits and test, if they are in the "Infinity-Area" etc... Read IEEE 754 to find out, what areas desribe which special values...

http://en.wikipedia.org/wiki/IEEE_float ... t_standard
I know how floating point stuff works. And writing code would be very easy, e.g.:

Code: Select all

Procedure IsNaN(f.f)
    If (PeekL(@f)&$7F80000)=$7F80000 And (PeekL(@f)&$007FFFF)<>0
        isnan=1
    EndIf
    ProcedureReturn isnan
endprocedure
But I'd rather have it as part of the language. Wouldn't it be easier to call IsNan() for any type of floating point value (assuming more than just SP floats are supported in future) rather than having to call IsNaNSP(), IsNaNDP(), IsNaNEP() etc and you as a programmer having to make that choice depending on whether you can remember what types your variables are?

*Edit: at least, the way I'd like to see them in PB. Ignore my explanation, I read your code wrongly. Sorry. PS, direct comparisons to NaN can't work in your code since the mantissa is not any specific value. You'd need bit masking.
Last edited by tinman on Fri Feb 04, 2005 11:40 pm, edited 1 time in total.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

@Andras
Isn't the positive and negative zero switched in your example, as bit 32 is used as sign bit?
Andras
User
User
Posts: 34
Joined: Wed Oct 27, 2004 6:58 am
Location: Germany

Post by Andras »

@Pupil: Yes, you're right!...

@TinMan: Sure, IsNAN() would be nice.
Undank ist der Welten Lohn
Post Reply