Nan - clarification lazarus / 21-General ---------------------------------------------------------------------- mtanner · 2021-01-02 11:29 I'm not sure what is to be expected when a variable is Nan. I can test if it's Nan with IsNan, fine I expected a program check if I code X:=Nan; X:=X+1; but it seems Nan is treated as zero as far as I can see. Is that correct? ---------------------------------------------------------------------- winni · 2021-01-02 11:46 Hi! Just a simple test Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var i : single;begini := nan;i := i +1;showMessage(FloatToStr(i));end;                       This shows NaN. What might be the result of Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ∞ + 1 Think about it. Winni ---------------------------------------------------------------------- wp · 2021-01-02 11:47 Quote from: mtanner on January 02, 2021, 12:29:02 pmNan is treated as zero as far as I can see. Is that correct? No, it should crash. You cannot calculate with NaN, or risk the exception. Check for NaN by calling IsNaN(x): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  x: Double;...  x := NaN;  if not IsNaN(x) then x := x + 1; ---------------------------------------------------------------------- ChrisR · 2021-01-02 12:06 NaNs can be signaling or quiet   https://randomascii.wordpress.com/2012/05/20/thats-not-normalthe- performance-of-odd-floats/ Any function that has a NaN as one of its operands should return a NaN. So NaN +.-.*./ any value should result in a NaN. While it might be useful to detect if a NaN occurred, we typically do not want the program to crash, rather we want to retain the value NaN. For example, in neuroimaging statistics, tools like SPM encode each voxel outside the brain as NaN indicating that we are not interested in these locations. ---------------------------------------------------------------------- mtanner · 2021-01-02 14:38 Well that makes sense, and yes I see that Nan+1=Nan I have a family of rather complicated functions that each retirn a  number of results (as a record). For some functions a particular result may not be relevent. I was hoping to set all elements of the vector (record) to Nan, then if I mistakenly tried to use a result in the calling application, but it had not been calculayed, then having  avalue of Nan it would cause a program exception, indicating a bug. There goes another good idea. ---------------------------------------------------------------------- lucamar · 2021-01-02 15:28 Quote from: winni on January 02, 2021, 12:46:25 pmWhat might be the result ofCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ∞ + 1 Note that NaN is not quite the same as Infinite (which would, BTW, be the result of ∞ + 1). NaN is intended to represent ... well, "not- numbers", like e.g. sqrt(-3), indeterminates like 0/0, meaningless ops with infinites (0 * ∞, ∞ - ∞), etc. Also, the (binary) floating-point representations of each are different ... ---------------------------------------------------------------------- Zoran · 2021-01-02 15:57 Quote from: mtanner on January 02, 2021, 03:38:49 pmWell that makes sense, and yes I see that Nan+1=Nan I have a family of rather complicated functions that each retirn a  number of results (as a record). For some functions a particular result may not be relevent. I was hoping to set all elements of the vector (record) to Nan, then if I mistakenly tried to use a result in the calling application, but it had not been calculayed, then having  avalue of Nan it would cause a program exception, indicating a bug. There goes another good idea. The idea doesn't have to go. To get the behaviour you expected, you can use AdvancedRecords -- hide the actual fields of the record in private section and allow accessing them through properties. In each property's getter method check if the actual value is nan and raise exception if it is. For example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MyRecord; {$mode objfpc}{$H+}    {$modeswitch advancedrecords on} interface uses  SysUtils; type   ENanAccessException = class(Exception);   TMyRecord = record  strict private    FValue1: Double;    FValue2: Double;     function GetValue1: Double;    function GetValue2: Double;     class procedure RaiseNanAccessException; static;   public    property Value1: Double read GetValue1 write FValue1;    property Value2: Double read GetValue2 write FValue2;   end; implementation function TMyRecord.GetValue1: Double;begin  if FValue1.IsNan then    RaiseNanAccessException;  Result := FValue1;end; function TMyRecord.GetValue2: Double;begin  if FValue2.IsNan then    RaiseNanAccessException;  Result := FValue2;end; class procedure TMyRecord.RaiseNanAccessException;begin  raise ENanAccessException.Create('Reading a NaN value!');end; end. ---------------------------------------------------------------------- Zoran · 2021-01-02 16:02 Quote from: lucamar on January 02, 2021, 04:28:12 pmQuote from: winni on January 02, 2021, 12:46:25 pmWhat might be the result ofCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ∞ + 1 Note that NaN is not quite the same as Infinite Wasn't it the point of Winni's question? Quote from: winni on January 02, 2021, 12:46:25 pmThink about it. Winni ---------------------------------------------------------------------- BeniBela · 2021-01-02 17:29 There was a time where even isNaN crashed when called on NaN ---------------------------------------------------------------------- jwdietrich · 2021-01-02 17:38 Quote from: lucamar on January 02, 2021, 04:28:12 pmNote that NaN is not quite the same as Infinite (which would, BTW, be the result of ∞ + 1). NaN is intended to represent ... well, "not-numbers", like e.g. sqrt(-3), indeterminates like 0/0, meaningless ops with infinites (0 * ∞, ∞ - ∞), etc. Also, the (binary) floating-point representations of each are different ... By definition, NaN is 0 / 0 as a real or extended number. Therefore, it has both properties of 0 and of ∞. See https://www.freepascal.org/docs-html/rtl/math/nan.html for reference. The Math unit also defines Infinity, which is 1 / 0. ---------------------------------------------------------------------- Warfley · 2021-01-02 17:59 NaN is a special value "Not A Number" and has some special properties, which are quite logical if you think about it. Let's say you want to do maths with a banana. A banana isn't a number, so one can say it is not a number. So what is banana + 1? Well, as banana isn't a number we can't compute anything about it, therefore this is also NaN. Same goes for -, * and /. About comparisons, it is similar. As NaN is unordered, it is not comparable to anything. So >, <, >=, <=, are not really defined. For = and <> is the same. You can see it as this, both a Banana and an Apple are not numbers, but still they are not the same. Therefore NaN = NaN does not hold in general. Similarly if you compare the same banana agianst itself, NaN <> NaN would also not hold in this case, so in general neither = nor <> can be defined. So neither of the comparison operators, <, <=, =, <>, >=, > can be well defined. So it depends on the implementation how they are handled. Some implementations just return false for any comparison against NaN, therefore NaN < a <=> NaN <= a <=> NaN = a <=> NaN <> a <=> NaN >= a <=> NaN > a <=> False, for any a (including NaN). Other implementations might raise an error/signal if one attempts to do anything with NaN. An implementation that would simply return random values would also be possible. Note that while NaN is specified in the IEEE 754 standard, it is also up the the language to handle NaNs, so for example while the IEEE 754 standard does say that comparisons even with quiet nans should signal an error, the GCC implementation for C and C++ handles NaN comparisons to always be false. Therefore the only correct and portable way to handle NaN is, if a function can return NaN, check the value if it is NaN (via isNaN), if it isn't, you can do maths with it, if it is NaN, don't touch it. Otherwise different languages, compilers or different hardware might handle NaN differently (or maybe even don't handle it at all) ---------------------------------------------------------------------- Paolo · 2021-01-02 22:28 in addition to what Warfley said, it depends also on the flags settings if the operation raises or not an exception (in delphi see setexceptionmask) in FPC not yet checked. ---------------------------------------------------------------------- Thaddy · 2021-01-03 05:21 In FreePascal too: https://www.freepascal.org/docs-html/rtl/math/setexceptionmask.html That is: if a processor family supports it.