Recent

Author Topic: Where could I find documentation on nan  (Read 8550 times)

franzala

  • New Member
  • *
  • Posts: 39
Where could I find documentation on nan
« on: January 16, 2021, 12:50:10 am »
In the Freepascal wiki, it is explained that NaN (not a number) is a numeric data type value representing an undefined or unrepresentable value.

But I noticed inadvertently that Round(nan) is equal to zero. (with FPC 3.0.4).

Is this the normal result or just an accident due to particular circumstances? In other words, may I use that result riskless?

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Where could I find documentation on nan
« Reply #1 on: January 16, 2021, 09:48:15 pm »
FPC 3.2.0, Windows, x64.
This program
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses Math, SysUtils;
  5.  
  6. var
  7.   D: ValReal = Nan;
  8. begin
  9.   try
  10.     Writeln(Round(Nan));
  11.     Writeln(Round(D));
  12.   except
  13.     on E: Exception do
  14.       Writeln(E.ClassName, ': ', E.Message);
  15.   end;
  16.   Readln;
  17. end.
outputs
Quote
-9223372036854775808
EInvalidOp: Invalid floating point operation
Thus, this is a feature of the compiler's constant calculation.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Where could I find documentation on nan
« Reply #2 on: January 16, 2021, 09:54:25 pm »
To test this, you have to turn exceptions off, see source below.  This is often done because certain external code (e.g. opengl) might assume to ignore floating point exceptions.

I tried on FPC 3.2.0 win32 and an older FPC 3.3.1 on win64, but got

Code: [Select]
-9223372036854775808
-9223372036854775808

as results in both cases

Code: Pascal  [Select][+][-]
  1.  
  2.     {$MODE OBJFPC}
  3.     {$APPTYPE CONSOLE}
  4.      
  5.     uses Math, SysUtils;
  6.      
  7.     var
  8.       D: ValReal = Nan;
  9.     begin
  10.      SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]) ;
  11.       try
  12.         Writeln(Round(Nan));
  13.         Writeln(Round(D));
  14.       except
  15.         on E: Exception do
  16.           Writeln(E.ClassName, ': ', E.Message);
  17.       end;
  18.       Readln;
  19. end.

It might be worth an bugreport since afaik all operations on nan should return nan.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Where could I find documentation on nan
« Reply #3 on: January 16, 2021, 10:40:14 pm »
It might be worth an bugreport since afaik all operations on nan should return nan.
In FPC, the Round function is of type Int64, so it cannot return Nan. In my opinion, Exception for such a case is normal. If you do not want an Exception, then you need to do IsNan checks.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Where could I find documentation on nan
« Reply #4 on: January 16, 2021, 10:42:01 pm »
It might be worth an bugreport since afaik all operations on nan should return nan.
In FPC, the Round function is of type Int64, so it cannot return Nan. In my opinion, Exception for such a case is normal. If you do not want an Exception, then you need to do IsNan checks.

True. You are right of course, the result of round is always an integer type, which has no NAN. So indeed you would have to check for NAN.

ArtLogi

  • Full Member
  • ***
  • Posts: 184
Re: Where could I find documentation on nan
« Reply #5 on: February 04, 2021, 08:20:19 pm »
Hello, NaN is part of original IEC 754 float definition. Dr. William Kahan was the big brains behind the current way of binary code floating points. Before it were wild and every CPU could have own implementation, some of them did have BCD, Crey or some form of 2^x with exponen and mantissa. Even today these NaN and infinities are poorly understood according to Dr. Kahan. Google it William Kahan is the key word for interesting stuff on current floats.

Ie. One paper from Kahans homepage.
https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
« Last Edit: February 04, 2021, 08:36:15 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Where could I find documentation on nan
« Reply #6 on: February 04, 2021, 08:37:52 pm »
@ArtLogi thanks for that interesting post, hence https://en.wikipedia.org/wiki/William_Kahan

Noting the links to a couple of HP Journal articles and slightly OT, I'd remind everybody of the wealth of technical information that's still available in these: unlike IBM, HP still has their journal archive accessible.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ArtLogi

  • Full Member
  • ***
  • Posts: 184
Re: Where could I find documentation on nan
« Reply #7 on: February 05, 2021, 12:11:29 am »
@ArtLogi thanks for that interesting post, hence https://en.wikipedia.org/wiki/William_Kahan

Noting the links to a couple of HP Journal articles and slightly OT, I'd remind everybody of the wealth of technical information that's still available in these: unlike IBM, HP still has their journal archive accessible.

MarkMLl
Thank you.  :) I'm merely a enthuastic amateur at best, who find subject interesting.

Another multiplatform language and article with valid points to consider. Is FPC as dangerous... :-X
https://people.eecs.berkeley.edu/~wkahan/JAVAhurt.pdf

While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Where could I find documentation on nan
« Reply #8 on: February 05, 2021, 02:36:42 am »
Isn't there anything like NaN for integer?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Where could I find documentation on nan
« Reply #9 on: February 05, 2021, 02:50:11 am »
Isn't there anything like NaN for integer?

Nope. All bit combinations of an integer have a regular meaning; that's, all represent a whole, existing number.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Where could I find documentation on nan
« Reply #10 on: February 05, 2021, 09:17:16 am »
Nope. All bit combinations of an integer have a regular meaning; that's, all represent a whole, existing number.
0 div 0 could be NaN for integers.  :)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Where could I find documentation on nan
« Reply #11 on: February 05, 2021, 09:29:32 am »
0 div 0 could be NaN for integers.  :)

Nah, it just makes both the CPU and the FPU go bonkers ;)

Besides, wouldn't that be 0? I mean, suppose you have no candy (or apples, oranges, cake, pizza, ...) to share among no friends, the result is easy: nothing at all, i.e. zero :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Where could I find documentation on nan
« Reply #12 on: February 05, 2021, 09:37:38 am »
Another multiplatform language and article with valid points to consider. Is FPC as dangerous... :-X
https://people.eecs.berkeley.edu/~wkahan/JAVAhurt.pdf

I think that what FPC does depends to a far greater extent on the default behaviour of the underlying platform. However I think it's fair to quote from that paper in case anybody else has an opinion.

Quote
Java’s floating-point arithmetic is blighted by five gratuitous mistakes:
1. Linguistically legislated exact reproducibility is at best mere wishful thinking.
2. Of two traditional policies for mixed precision evaluation, Java chose the worse.
3. Infinities and NaNs unleashed without the protection of floating-point traps and flags
mandated by IEEE Standards 754/854 belie Java’s claim to robustness.
4. Every programmer’s prospects for success are diminished by Java’s refusal to grant access
to capabilities built into over 95% of today's floating-point hardware.
5. Java has rejected even mildly disciplined infix operator overloading, without which extensions
to arithmetic with everyday mathematical types like complex numbers, intervals, matrices,
geometrical objects and arbitrarily high precision become extremely inconvenient.
To leave these mistakes uncorrected would be a tragic sixth mistake.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Where could I find documentation on nan
« Reply #13 on: February 05, 2021, 01:50:35 pm »
0 div 0 could be NaN for integers.  :)

Nah, it just makes both the CPU and the FPU go bonkers ;)

Besides, wouldn't that be 0? I mean, suppose you have no candy (or apples, oranges, cake, pizza, ...) to share among no friends, the result is easy: nothing at all, i.e. zero :D
No, it's not 0, it's undefined. As 0.0/0.0 is also undefined. Both throw exceptions in FPC,

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Where could I find documentation on nan
« Reply #14 on: February 05, 2021, 02:07:14 pm »
Nope. All bit combinations of an integer have a regular meaning; that's, all represent a whole, existing number.
0^0 is undefined as well IIRC?

Bart

 

TinyPortal © 2005-2018