Recent

Author Topic: IsNan  (Read 1321 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 350
IsNan
« on: February 08, 2025, 05:54:45 am »
This does not work. Does not change value of r1.
Code: Pascal  [Select][+][-]
  1. if IsNan(circle.r1) then circle.r1 := circle.r;
  2.  

If I inspect circle on breakpoint it shows r1 = Nan, r = 200

?
lazarus 3.2-fpc-3.2.2-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: IsNan
« Reply #1 on: February 08, 2025, 08:53:35 am »
This does not work.
Work:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$IFDEF WINDOWS}
  3.   {$AppType CONSOLE}
  4. {$ENDIF}
  5.  
  6. uses Math;
  7.  
  8. var
  9.   R1: ValReal = Nan;
  10.   R2: ValReal = 200;
  11. begin
  12.   if IsNan(R1) then
  13.     R1 := R2;
  14.   Writeln(R1:0:2);
  15.   Readln;
  16. end.
Output 200.00. Windows, FPC 3.2.2 x64

Paolo

  • Hero Member
  • *****
  • Posts: 573
Re: IsNan
« Reply #2 on: February 08, 2025, 09:24:09 am »
Maybe written the code in this way  ie if... then on the same row at breakpoint time only the value in the if is shown, try putting the break point on the next row and see... or even better split the code on two rows, maybe

wp

  • Hero Member
  • *****
  • Posts: 12696
Re: IsNan
« Reply #3 on: February 08, 2025, 11:02:56 am »
Yes, the code of the line marked by a breakpoint has not yet been executed when the program stops here: https://wiki.freepascal.org/IDE_Window:_Breakpoints -"The execution will be interrupted before the pascal statement on the specified line is executed". Press F8, and the changes made in that line will have been made.

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 350
Re: IsNan
« Reply #4 on: February 08, 2025, 11:21:29 am »
No. It is not that (past breakpoint theory). Have no idea how it gets Nan value, there, at all.

Test, that for some reason work, and r1 is not Nan, it is 5.14209214845029E-4935

I have, same code as below, at diff spot,  but r1 comes out as Nan.

Code: Pascal  [Select][+][-]
  1. type
  2.         Tcircle = record
  3.                 r: Float; // axis minor
  4.                 r1: Float;// axis mayor
  5.                 center: TFloatPoint;
  6.         end;
  7.  
  8.  
  9. var
  10.    circle: Tcircle;
  11. begin
  12.    circle.center.X := 0;
  13.    circle.center.y := 0;
  14.    circle.r := 200;
  15.  
  16.    if IsNan(circle.r1) then circle.r1 := circle.r;
  17.    showmessage(FloatToStr(circle.r1));
  18. end;  
  19.  
  20.  
« Last Edit: February 08, 2025, 11:25:17 am by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12696
Re: IsNan
« Reply #5 on: February 08, 2025, 11:44:31 am »
In "if IsNaN(circle.r1)" you are accessing the field r1 of the circle record without having initialized it before - the result of the "if" is not determined.

Paolo

  • Hero Member
  • *****
  • Posts: 573
Re: IsNan
« Reply #6 on: February 08, 2025, 11:46:04 am »
But you are speaking about two different things. In the first post you had problems with breakpoints, and we gave you an answer,  in the second case you didn't set the variable, then the variable content can be anything. Look at nan bit pattern definition.
Wp was Faster than me.

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 350
Re: IsNan
« Reply #7 on: February 08, 2025, 06:52:39 pm »
As said, there is no 'problems with breakpoints'.

Quote
variable content can be anything

Including 'Nan' ?

Actual part of code that produces 'Nan'
Same as above, but instead anything in range of Float, gives out Nan.

Code: Pascal  [Select][+][-]
  1. procedure TForm2.Button8Click(Sender: TObject);
  2. var
  3.   FParser: TFPExpressionParser;
  4.   parserResult: TFPExpressionResult;
  5.   Parser: TFpExpressionParser;
  6.   mathExpression:AnsiString;
  7.  
  8.   angle,angle1,angle2,angle3,x,y,x1,y1,tmp: float;
  9.   circle:Tcircle;
  10.   toCanvasCenterX,toCanvasCenterY: integer;
  11.    a,b,m,c,p,q:float;
  12.    circleRcutsRlipse: Boolean;
  13.    pT1,pT2:PointDecimal;
  14.    fP1,fP2,fP3,lineEndPoint1,lineEndPoint2: TFloatPoint;
  15.  
  16.    tmpStr: string;
  17.    distance: float;
  18.    slope,n,k,hipotenuza,rSlope: float;
  19.    tangentsPoints:TFloatPointArray;
  20.    chordPoints:TFloatPointArray;
  21.  
  22.    imageDragHolder: Timage;
  23.  
  24.    i: integer;
  25. begin
  26.     Parser := TFpExpressionParser.Create(self);
  27.     //definiraš da bo sprejel matematične izraze
  28.     Parser.Builtins := [bcMath];   // or FParser.Builtins := FParser.Builtins + [bcMath];
  29.  
  30.  
  31.     circle.center.X := 0;
  32.     circle.center.y := 0;
  33.     circle.r := 200;
  34.  
  35.     showmessage(FloatToStr(circle.r1));
  36.  




« Last Edit: February 08, 2025, 06:57:23 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

Paolo

  • Hero Member
  • *****
  • Posts: 573
Re: IsNan
« Reply #8 on: February 08, 2025, 06:58:55 pm »
Quote
Including 'Nan' ?

Yes

egsuh

  • Hero Member
  • *****
  • Posts: 1563
Re: IsNan
« Reply #9 on: February 09, 2025, 08:20:19 am »
How is NaN implemented at all, for single or double types? I wish I could define NaN or undefined for integer or real type variable, but it is not possible in typed language like Pascal.

Paolo

  • Hero Member
  • *****
  • Posts: 573
Re: IsNan
« Reply #10 on: February 09, 2025, 09:34:34 am »
At my knowledge NaN is defined for floating point numbers, it has its bits-pattern.
Look on internet for ieee 754 standard.

robert rozee

  • Sr. Member
  • ****
  • Posts: 259
Re: IsNan
« Reply #11 on: February 09, 2025, 12:07:43 pm »
How is NaN implemented at all, for single or double types? I wish I could define NaN or undefined for integer or real type variable, but it is not possible in typed language like Pascal.

that is a rather good question... every floating point number is encoded into a block of some number of bits. lets look at the example of a DOUBLE, that takes up 64 bits. the block of bits is divided up into THREE groups:
  • sign: 1 bit
  • exponent: 11 bits
  • mantissa: 52 bits (plus an assumed leading "1.")

see: https://en.wikipedia.org/wiki/Floating-point_arithmetic

sign is obvious: the number is either positive or negative. so + or
mantissa is the digits of the number that you would normally see written down, with an assumed "1." at the start (if it were "0." at the start then we could just shift the bits left to get it back to "1." and adjust the exponent accordingly, so we can always assume "1.")
exponent a sort of multiplier, which moves the "." left or right by some number of places. because we have 11 bits, and can move left or right, we have a range of -1024 to +1023, representing moving the "." left or right by this many places. another way to view it is as the exponent represents 2-1024 to 21023, which then is multiplied by the sign+mantissa to get back the encoded number. note: 20 equals 1, which just means "leave the decimal point where it already is".

now, you will see that the range of numbers below 1 that can be represented is twice as wide as the range of numbers above 1 that can be represented. we make use of this to 'borrow' a whole range of number for 'special' purposes. we co-opt the exponent number containing all 1's (11111111111 in the case of a DOUBLE) to have the special meaning of "not a number", while the bits of the mantissa now being flags that can signify a whole load of different things, such as NaN, infinity, underflow, and whatever else we wish.


cheers,
rob   :-)


« Last Edit: February 09, 2025, 12:54:06 pm by robert rozee »

 

TinyPortal © 2005-2018