Page 1 of 1

NaN() and if compare problem

Posted: Fri Feb 09, 2024 4:32 pm
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

Re: NaN() and if compare problem

Posted: Fri Feb 09, 2024 6:00 pm
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.

Re: NaN() and if compare problem

Posted: Fri Feb 09, 2024 6:25 pm
by juergenkulow

Re: NaN() and if compare problem

Posted: Fri Feb 09, 2024 8:37 pm
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.