NaN() and if compare problem

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

NaN() and if compare problem

Post by mk-soft »

ASM and C-backend test ...

Code: Select all

Define.d Number, Number2 = 1.2

; ASM OK, C-Backend Bug?
Number = Sqr(-1)
If Number = NaN()
  Debug "If / Ok: Number = NaN()"
Else
  Debug "Else / Bug: Number = NaN()"
EndIf

; ASM OK, C-backend Ok
Number = Sqr(-1)
If IsNAN(Number)
  Debug "If / Ok: IsNaN(Number)"
Else
  Debug "Else / Bug: IsNaN(Number)"
EndIf

; ASM Bug, C-Backend Bug
Number2 = 1.2
If Number = Number2
  Debug "If / Bug: Number = Number2"
Else
  Debug "Else / Ok: Number = Number2"
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4805
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: NaN() and if compare problem

Post by Little John »

mk-soft wrote:

Code: Select all

; ASM OK, C-Backend Bug?
Number = Sqr(-1)
If Number = NaN()
[...]
Wrong code. In order to check for NaN, only use PB's IsNaN() function.
Documentation wrote: NaN is a special value. Testing for it should not be done using normal comparisons because there are actually many different values for NaN and whether or not NaN is considered equal with itself in comparisons depends on the hardware.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: NaN() and if compare problem

Post by juergenkulow »

DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: NaN() and if compare problem

Post by DarkDragon »

The inequality of NaN against NaN is the early version of the IsNaN check. So if x <> x it is NaN. But it should be the same on all backends.
bye,
Daniel
Post Reply