Why MaxDouble does not equal to MaxDouble? lazarus / 21-General ---------------------------------------------------------------------- jollytall · 2024-10-04 18:45 I have a simple program: 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 uses Math;var d : double;begind := MaxDouble;writeln(d, ' ', MaxDouble, ' ', d = MaxDouble);end. The result is Code: [Select] 1.7976931348623157E+308  1.79769313486231569996E+0308 FALSE Why is MaxDouble shown as E+0308, when even in the source it is defined as 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     const    { values according to        https://en.wikipedia.org/wiki/Double- precision_floating-point_format#Double-precision_examples      }      MinDouble    =  2.2250738585072014e-308;      MaxDouble    =  1.7976931348623157e+308;What is the best way to have a standard, built-in rarely used number to indicate that the number is not really used? ---------------------------------------------------------------------- Warfley · 2024-10-04 19:14 Thats the great thing about untyped constants, for floats they always are interpreted as extended. So when you put it into a double, it will get converted to double. When you than compare the double against the constant, the double will be promoted to extended. This double (pun intended) conversion creates rounding errors. ---------------------------------------------------------------------- jollytall · 2024-10-04 19:55 It is clear, but then I would consider it a bug. Shouldn't unit Math has it like: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 const MaxDouble : double = 1.7976931348623157e+308; ---------------------------------------------------------------------- Warfley · 2024-10-04 20:05 This introduces a memory object (typed consts are objects in the executable, while untyped are just "macros" that are replaced by the compiler at compiletime), I think the correct way would be: 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     const MaxDouble = Double(1.7976931348623157e+308); ---------------------------------------------------------------------- jollytall · 2024-10-04 20:13 Oh, yes, that's how it should be. ---------------------------------------------------------------------- Zoran · 2024-10-04 22:03 Instead of old Math.MaxDouble, use Double.MaxValue. You only need SysUtils in uses, for type helper. It is declared as typed constant: https://www.freepascal.org/docs- html/current/rtl/sysutils/tdoublehelper.maxvalue.html ---------------------------------------------------------------------- mtrsoft · 2024-10-05 05:38 The above replies are all correct. A few additional minor observations. 1. You are running Windows x86 which supports the FPU and the Extended type. 2. On Windows x64 and 64 bit apps the Extended type is an alias for the Double type so there shouldn't be an issue. 3. One reason for sticking with 32-bit apps is that the Extended type and FPU are supported. 4. The Extended type has 19 to 20 significant digits whle the Double type has only 14 to 15 significant digits. (I know most people seldom need that many sig. fig., but....) 5. The Extended type has a greater dynamic range of a bit more the e+/-4000. (Again, not often needed, but can be useful.) John ---------------------------------------------------------------------- jollytall · 2024-10-05 07:40 I actually use Linux 64 bit, and I always use Double as my "standard" floating point type (maybe a bad practice? thoughts?). Double should be by definition 64 bits, shouldn't it? Extended can be 80 bits on x64, so how could they be the same under Windows? A related question: I also use Integer as my "standard" integer type as an old habit. Is it actually slower than using Int64 on an x64 platform, or does not matter? As Integer is a variable type being SmallInt or LongInt based on the processor (16bit or 32 or more bits), shouldn't it be "even more variable", being SmallInt (16bit), LongInt(32bit) or Int64(64bit) depending on the system? ---------------------------------------------------------------------- Warfley · 2024-10-05 09:59 Quote from: mtrsoft on October 05, 2024, 07:38:52 am1. You are running Windows x86 which supports the FPU and the Extended type. 2. On Windows x64 and 64 bit apps the Extended type is an alias for the Double type so there shouldn't be an issue. Depending on your FPC configuration, the fpc can simulate 80bit float operations in software if no FPU support is available, so this is not necessarily true. ---------------------------------------------------------------------- tetrastes · 2024-10-05 11:08 Quote from: Warfley on October 05, 2024, 11:59:53 amQuote from: mtrsoft on October 05, 2024, 07:38:52 am1. You are running Windows x86 which supports the FPU and the Extended type. 2. On Windows x64 and 64 bit apps the Extended type is an alias for the Double type so there shouldn't be an issue. Depending on your FPC configuration, the fpc can simulate 80bit float operations in software if no FPU support is available, so this is not necessarily true. Really? I would prefer that fpc uses 80 bit float operations at win64, where x87 fpu is present. May be you know how to configure this? ---------------------------------------------------------------------- BrunoK · 2024-10-05 11:11 Quote from: mtrsoft on October 05, 2024, 07:38:52 amThe above replies are all correct. 2. On Windows x64 and 64 bit apps the Extended type is an alias for the Double type so there shouldn't be an issue. 3. One reason for sticking with 32-bit apps is that the Extended type and FPU are supported. 4. The Extended type has 19 to 20 significant digits whle the Double type has only 14 to 15 significant digits. (I know most people seldom need that many sig. fig., but....) 5. The Extended type has a greater dynamic range of a bit more the e+/-4000. (Again, not often needed, but can be useful.) FPC x86_64 for Windows has crippled the possibilities of the x86_64 processor suppressing the precision of 80 bits floats as described in https://en.wikipedia.org/wiki/Extended_precision, search paragraph x86 extended precision format. The wrong assessment of the specifics of 80 bits FPU usage led to the WRONG multiplications of currencies for large values on the windows x86_64 64 bit platform. ---------------------------------------------------------------------- Thaddy · 2024-10-05 11:17 Florian did give some instructions on how to build FPC Win64 with 80 bit float support a very, very, very long time ago. But you can run into trouble soon, because the Win64 ABI expects extended to be double! https://learn.microsoft.com/en-us/cpp/build/x64-calling- convention?view=msvc-170 A small amount of work has been done to provide 128, 256 and 512 bit return types for floating point operations, so it is certainly possible to write code with a much higher precision than 80 bits. At the moment that requires inline assembler, though, afaik. See the details in the link. ---------------------------------------------------------------------- tetrastes · 2024-10-05 12:15 Strange, but I don't run into trouble with gcc. I don't understand why fpc team took such decision. At least, they could suggest another option for users. I think, that the same numerical code giving different results at x86_64-win64 and x86_64-linux is not good. ---------------------------------------------------------------------- marcov · 2024-10-05 12:23 Quote from: jollytall on October 05, 2024, 09:40:23 amI actually use Linux 64 bit, and I always use Double as my "standard" floating point type (maybe a bad practice? thoughts?). Double should be by definition 64 bits, shouldn't it? Extended can be 80 bits on x64, so how could they be the same under Windows? Windows x86_64 ABI deprecated the x87 CPU, and specifies to use SSE2 for floating point. ---------------------------------------------------------------------- BrunoK · 2024-10-05 12:54 Quote from: Thaddy on October 05, 2024, 01:17:37 pmFlorian did give some instructions on how to build FPC Win64 with 80 bit float support a very, very, very long time ago. Where, link please. QuoteBut you can run into trouble soon, because the Win64 ABI expects extended to be double! https://learn.microsoft.com/en-us/cpp/build/x64-calling- convention?view=msvc-170 Sorry, this article does not push out anything related to the x87. It specifies how and in which order parameters are passed to sytem calls, how they are aligned. It tells about how the 87 FPU registers must be handled. As a matter of fact, 87 FPU are inlined to the code stream and NOT passed to the OS. The only ABI convention that would be of FPC consideration is that the Extended type should be stored on a 16 byte alignment with byte9 at memory byte15 offset if calling the system, but there are no (to my knowledge) 64 bit window calls using EXTENDED. We are in a situation were progress is REGRESSION ! ---------------------------------------------------------------------- tetrastes · 2024-10-05 13:35 Quote from: BrunoK on October 05, 2024, 02:54:30 pmWe are in a situation were progress is REGRESSION ! Sic! ---------------------------------------------------------------------- marcov · 2024-10-05 14:03 Quote from: BrunoK on October 05, 2024, 02:54:30 pmSorry, this article does not push out anything related to the x87. It specifies how and in which order parameters are passed to sytem calls, how they are aligned. It tells about how the 87 FPU registers must be handled. I only see that st0..st7 is saved across calls. Do I miss something where x87 is specified? All float examples seems to use SSE2 XMM. It is a long article, so maybe I misread, but could you quote the relevant bits ? ---------------------------------------------------------------------- BrunoK · 2024-10-05 14:33 Quote from: marcov on October 05, 2024, 04:03:20 pmI only see that st0..st7 is saved across calls. Do I miss something where x87 is specified? All float examples seems to use SSE2 XMM. It is a long article, so maybe I misread, but could you quote the relevant bits ? All float examples seems to use SSE2 XMM. Because Windows does not use the 87 FPU. The alignment, if it was required for a call to Windows as i expose derives from : Quotefunc4(__m64 a, __m128 b, struct c, float d, __m128 e, __m128 f); // a in RCX, ptr to b in RDX, ptr to c in R8, d in XMM3, // ptr to f pushed on stack, then ptr to e pushed on stack _m128 is passed by reference. Somewhere else in MS doc it is stated that things are aligned on 64 bit. Some should be aligned on 128 bit, but here the fun is that cmem loses the 16 byte align due to the memsize being stored at the beginning of the allocated block ... That causes absolutely no trouble. Anyway the Windows ABI has nothing to say about how the compiler should lay out its generated code. x87 FPU instructions are inlined by the compiler in the generated code (as is probably done in linux x86_64 or i386 floats). ---------------------------------------------------------------------- BrunoK · 2024-10-05 14:38 Quote from: marcov on October 05, 2024, 02:23:09 pmWindows x86_64 ABI deprecated the x87 CPU, and specifies to use SSE2 for floating point. Link ? ---------------------------------------------------------------------- marcov · 2024-10-05 15:06 Quote from: BrunoK on October 05, 2024, 04:38:06 pmQuote from: marcov on October 05, 2024, 02:23:09 pmWindows x86_64 ABI deprecated the x87 CPU, and specifies to use SSE2 for floating point. Link ? They were Windows XP era documents in the period before Vista came out, MSDN has been redone several times over.  However wikipedia still hints on it Quote from: https://en.wikipedia.org/wiki/X86-64The following additional characteristics apply to all x64 versions of Windows: ... Early reports claimed that the operating system scheduler would not save and restore the x87 FPU machine state across thread context switches. Observed behavior shows that this is not the case: the x87 state is saved and restored, except for kernel mode-only threads (a limitation that exists in the 32-bit version as well). The most recent documentation available from Microsoft states that the x87/MMX/3DNow! instructions may be used in long mode, but that they are deprecated and may cause compatibility problems in the future.[110] (3DNow! is no longer available on AMD processors, with the exception of the PREFETCH and PREFETCHW instructions,[112] which are also supported on Intel processors as of Broadwell.) IIRC this was never revoked for Windows XP64. Note also that the remark that it wasn't true was tested experimentally. It took quite a while before it was documented. ---------------------------------------------------------------------- BrunoK · 2024-10-05 15:38 Quote from: marcov on October 05, 2024, 05:06:48 pmThey were Windows XP era documents in the period before Vista came out, MSDN has been redone several times over.  However wikipedia still hints on it Wikipedia, hum ... very up to date. Quote from: https://en.wikipedia.org/wiki/X86-64The following additional characteristics apply to all x64 versions of Windows: ... Early reports ... etc .. Its a bit like when they do modelling with climate or did covid modelling but couldn't reproduce results on different machines. I always suspected that calculation accuracy was involved. If you read and follow the link (wiki mind you ...)  that I mentioned above, you may grasp a bit of what I understand about that 54/63 bit mantissa and a small introduction why the 87 FPU settled for 80 bit extended, money, technological capacity and ACCURACY. There is no reason to castrate our programs in the name of "Windows support only 64 bit floats". QuoteIIRC this was never revoked for Windows XP64. Note also that the remark that it wasn't true was tested experimentally. It took quite a while before it was documented. Word salad, what was revoked and what and where was it stated, documented ? ---------------------------------------------------------------------- marcov · 2024-10-05 15:46 Quote from: BrunoK on October 05, 2024, 05:38:18 pmQuoteIIRC this was never revoked for Windows XP64. Note also that the remark that it wasn't true was tested experimentally. It took quite a while before it was documented. Word salad, what was revoked and what and where was it stated, documented ? I don't understand. You want the date it was not revoked ? ---------------------------------------------------------------------- BrunoK · 2024-10-05 15:50 Quote from: marcov on October 05, 2024, 05:46:55 pmI don't understand. You want the date it was not revoked ?  A quote from some valid source that states what you describe ? Do not try to drown the fish, please. ---------------------------------------------------------------------- BrunoK · 2024-10-05 15:59 Maybe some one could correct Wikipedia entry re Extended entry that states QuoteTurbo Pascal (and Object Pascal or Delphi) has an extended 80 bit type available in addition to real / single (32 bits) and double (64 bits), either natively (when a 80x87 coprocessor is present) or emulated (through the Turbo87 library); this extended type is available on 16, 32, and 64 bit platforms, possibly with padding.[32]And clarify the the windows 64 bit version doesn't support 80 bit floats with processors equiped with 87 FPU. ---------------------------------------------------------------------- BrunoK · 2024-10-05 16:06 Quote from: BrunoK on October 05, 2024, 05:59:19 pmMaybe some one could correct Wikipedia entry re Extended entry that states QuoteTurbo Pascal (and Object Pascal or Delphi) has an extended 80 bit type available in addition to real / single (32 bits) and double (64 bits), either natively (when a 80x87 coprocessor is present) or emulated (through the Turbo87 library); this extended type is available on 16, 32, and 64 bit platforms, possibly with padding.[32]And clarify the the windows 64 bit version doesn't support 80 bit floats with processors equiped with 87 FPU. Correct : And clarify the Windows 64 bit version of the Free Pascal Compiler doesn't support 80 bit floats with processors equiped with 87 FPU. ---------------------------------------------------------------------- Thaddy · 2024-10-05 16:19 As I wrote before: you can support the fpu on win64 but it takes a recompile and Florian gave the instructions for it. If it is sane to do so is a second question. You can detect some support for higher precision, because on 64 bit AMD/Intel vectorcall is already supported and there are some type definitions for 128 - a packed record with two 64 bit values - HVA. I think 256 bit and 512 bit are still missing as is much of the compiler infrastructure to work with them, although the inline assembler code is already supported, so the appropiate registers - ABI conformant - will work for xmm and possibly ymm. So it is not as gloomy as you write. ---------------------------------------------------------------------- BrunoK · 2024-10-05 16:24 Quote from: Thaddy on October 05, 2024, 06:19:25 pmAs I wrote before: you can support the fpu on win64 but it takes a recompile and Florian gave the instructions for it. Where are these instrructions, I've asked you repetidively and many times but you never supplied the information. Repeat : You never supplied the information QuoteIf it is sane to do so is a second question. You can detect some support for higher precision, because on 64 bit AMD/Intel vectorcall is already supported and there are some type definitions for 128 - a packed record with two 64 bit values - HVA. I think 256 bit and 512 bit are still missing as is much of the compiler infrastructure to work with them, although the inline assembler code is already supported, so the appropiate registers - ABI conformant - will work for xmm and possibly ymm. So it is not as gloomy as you write. Word salad. ---------------------------------------------------------------------- BrunoK · 2024-10-05 16:34 https://gitlab.com/freepascal.org/fpc/source/-/issues/40928 It's not me ... ---------------------------------------------------------------------- Thaddy · 2024-10-05 16:51 If you can not find it on the forum, it is probably on the mailing list. I could not find it quickly  myself too, but I know it is there. ---------------------------------------------------------------------- BrunoK · 2024-10-05 17:04 Quote from: Thaddy on October 05, 2024, 06:51:43 pmIf you can not find it on the forum, it is probably on the mailing list. I could not find it quickly  myself too, but I know it is there. Much Talk. No show. ;-) ---------------------------------------------------------------------- Thaddy · 2024-10-05 17:47 Not, yet, but it should not be difficult to find. ---------------------------------------------------------------------- bytebites · 2024-10-05 17:53 Is there a bingo program too? ---------------------------------------------------------------------- tetrastes · 2024-10-05 17:57 From https://learn.microsoft.com/en-us/cpp/build/ieee-floating-point- representation?view=msvc-170: QuoteThe IEEE standard also specifies half-precision (2-byte) and quadruple-precision (16-byte) formats, and a double-extended-precision (10-byte) format, which some C and C++ compilers implement as the long double data type. In the MSVC compiler, the long double data type is treated as a distinct type, but the storage type maps to double. There is, however, intrinsic and assembly language support for computations using the other formats, including the double-extended-precision format, where supported by hardware. So Windows x86_64 ABI does not deprecate the x87 CPU, it, say that, doesn't bother much about that. ---------------------------------------------------------------------- Thaddy · 2024-10-05 19:07 Quote from: bytebites on October 05, 2024, 07:53:00 pmIs there a bingo program too? Well at least the algorithm demo was already published and and that did everybody else wrong. It will come. ---------------------------------------------------------------------- Zoran · 2024-10-06 04:31 Quote from: BrunoK on October 05, 2024, 07:04:44 pmQuote from: Thaddy on October 05, 2024, 06:51:43 pmIf you can not find it on the forum, it is probably on the mailing list. I could not find it quickly  myself too, but I know it is there. Much Talk. No show. ;-) Now this is really rude. Thaddy told you he remembers there were some instructions that might interest you. Although he could not find it, the fact that he had seen it once might be helpful to you, so you can try to search, if you are interested. He might help more and try to find it, but he certainly does not owe that to you. ---------------------------------------------------------------------- TRon · 2024-10-06 05:09 I was able to find this though expressed by PascalDragon. Took about 15 second to find with ddg ---------------------------------------------------------------------- BrunoK · 2024-10-06 10:26 Quote from: Zoran on October 06, 2024, 06:31:35 amNow this is really rude. Thaddy told you he remembers there were some instructions that might interest you. Thaddy bla bla remembers bla bla has written knows all was chief bla bla others are idiots blabla ready to run bla bla. Thaddy knows all. What Thaddy puts here is usually buggy and very very occasionally ok. ---------------------------------------------------------------------- BrunoK · 2024-10-06 10:28 Quote from: TRon on October 06, 2024, 07:09:41 amI was able to find this though expressed by PascalDragon. Took about 15 second to find with ddg  Thank you, noted and investigating based on your reply and asssociated link. B. ---------------------------------------------------------------------- BrunoK · 2024-10-06 17:15 Quote from: TRon on October 06, 2024, 07:09:41 amI was able to find this though expressed by PascalDragon. Took about 15 second to find with ddg  That is the decisive link. -dFPC_SUPPORT_X87_TYPES_ON_WIN64 when rebuilding FPC (3.2.2 on win64) solves also the Currency * Currency ending in wrong values for large numbers. Best of all worlds, uses 64bit double for Double calculations, and Extended for Extended data types. I didn't dig for other 87FPU functions. Doesn't look to be penalizing in terms of speed. Thanks. ---------------------------------------------------------------------- PascalDragon · 2024-10-07 19:22 Quote from: mtrsoft on October 05, 2024, 07:38:52 am3. One reason for sticking with 32-bit apps is that the Extended type and FPU are supported. Then you also shouldn't use non-x86 processors, because the x86 family is the only one that supports 80-bit floating point (well, m68k supports it as well, but it has a slightly different format). ---------------------------------------------------------------------- BrunoK · 2024-10-07 19:50 Quote from: PascalDragon on October 07, 2024, 09:22:36 pmQuote from: mtrsoft on October 05, 2024, 07:38:52 am3. One reason for sticking with 32-bit apps is that the Extended type and FPU are supported. Then you also shouldn't use non-x86 processors, because the x86 family is the only one that supports 80-bit floating point (well, m68k supports it as well, but it has a slightly different format). So you also should throw 80 bit float out or Linux x86_64 FPC. Why dont you do it ? ---------------------------------------------------------------------- PascalDragon · 2024-10-07 20:00 Quote from: BrunoK on October 07, 2024, 09:50:55 pmWhy dont you do it ? Simple: Backwards compatibility. ---------------------------------------------------------------------- BrunoK · 2024-10-07 20:02 Quote from: PascalDragon on October 07, 2024, 10:00:05 pmQuote from: BrunoK on October 07, 2024, 09:50:55 pmWhy dont you do it ? Simple: Backwards compatibility. Nice Joke. ---------------------------------------------------------------------- MarkMLl · 2024-10-07 20:16 Quote from: Zoran on October 06, 2024, 06:31:35 amNow this is really rude. Thaddy told you he remembers there were some instructions that might interest you. I suggest that "Go Forth and multiply", implying postfix notation, is not entirely in the spirit of a forum devoted to Pascal >:-) MarkMLl