Got Floating point error ... Expected Integer error lazarus / 21-General ---------------------------------------------------------------------- Ten_Mile_Hike · 2024-06-30 21:25 Why is the error generated  "Invalid floating point operation" and NOT 'Integer Overflow' or" Invalid Integer operation" 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 procedure TForm1.Button1Click(Sender:TObject);var I64:int64;begin  I64:= 10**19;     // The power function is (Int64 ** Int64): Int64end; ---------------------------------------------------------------------- jamie · 2024-06-30 23:23 The helper looks broken. It's always calling the Double version. If you put the break point on that line and open the ASM view window in the debug stuff, you can see where its calling the double version. P.S.   I meant the Overload. ---------------------------------------------------------------------- Thaddy · 2024-07-01 06:56 [edit] I compiled for the wrong architecture I also have problems with math in trunk from today. Given x86_64-win64 optimized for coreavx2, the following code seems broken: ---------------------------------------------------------------------- Zvoni · 2024-07-01 08:09 FPC3.2.2-32-Bit 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 program Project1;Uses SysUtils, Math; Var I64:Int64; begin  I64:=10**19;end.  Quote004016C1 31d2                     xor    %edx,%edx 004016C3 8945f8                   mov    %eax,-0x8(%ebp) 004016C6 8955fc                   mov    %edx,-0x4(%ebp) 004016C9 df6df8                   fildll -0x8(%ebp) 004016CC 8d6424f4                 lea    -0xc(%esp),%esp 004016D0 db3c24                   fstpt  (%esp) 004016D3 b813000000               mov    $0x13,%eax 004016D8 e8c31e0100               call   0x4135a0 004016DD df7df8                   fistpll -0x8(%ebp) 004016E0 9b                       fwait 004016E1 8b45f8                   mov    -0x8(%ebp),%eax 004016E4 8b55fc                   mov    -0x4(%ebp),%edx 004016E7 a310a04100               mov    %eax,0x41a010 004016EC 891514a04100             mov    %edx,0x41a014 Without Math-Unit i get "Operator not overloaded for Shortint ** Shortint" ---------------------------------------------------------------------- Thaddy · 2024-07-01 12:39 That is correct and on purpose: the overloaded operator **  is only in math. But it can be done with only system. Will add example later. ---------------------------------------------------------------------- Zvoni · 2024-07-01 12:44 Quote from: Thaddy on July 01, 2024, 02:39:50 pmThat is correct and on purpose: the operator is only in math. But it can be done with only system. Will add example later. It was more to point out: Where in blazes does the Statement "It uses the Double-Version" comes from? In my quote from the Assembler window i clearly see "intpower" and "longint", though no idea what the "extended" is supposed to mean. Though i did get a SIGFPE (!) when i executed above code ---------------------------------------------------------------------- Thaddy · 2024-07-01 13:25 This one uses only system: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 {$MODE OBJFPC}{$APPTYPE CONSOLE}{ overload ** only with system calls }operator ** (const a,b:int64):int64;begin  Result := Round(Exp(ln(a) * b));end; var  c:int64 = 2;  d:int64 = 8;begin  writeln(c ** d); // 256end.Is that what you mean? But the limit is high(int64) and 10 ** 19 does not fit in that range at all. no wonder you get an error 207 For your case the limit is 10 ** 18 on a 64 bit cpu. on 32 bit windows it might work with extended. ---------------------------------------------------------------------- jamie · 2024-07-01 21:03 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 Function IntPower(A,B:Int64):Int64;var  P,I:Int64;Begin P := 1; For I := 0 to B-1 do  P *=A; Result := P;end; ---------------------------------------------------------------------- Thaddy · 2024-07-02 05:27 Quote from: jamie on July 01, 2024, 11:03:28 pm Indeed shockingly bad code you you did not test for 10 and 19. It does not crash, but it gives a negative value because it overflows. -8446744073709551616 The power of 2 positive values can not be? ............ Fill your answer in there. That's the worst answer today Jamie. That code is useless.. The aswer should be 10.000.000.000.000.000.000 If you replace every int64 in your code with qword, all of them, your code works correct for 19, but then it fails on 20.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 Function UIntPower(A,B:qword):qword;var I:qword;Begin Result := 1; For I := 0 to B-1 do  Result *=A;end;   The limit for int64 is    9.223.372.036.854.775.807 So the limit is really 18. My code gives for 18 1000000000000000001 on Linux64 and a slightly higher error on Win64. Rounding error is:       0,0000000000000001% for 18 your code has no error, but it overflows for 19. ---------------------------------------------------------------------- Thaddy · 2024-07-02 07:04 For powers of 10 with strings: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 function powten(n:Integer):string;var i:integer;begin  Result:='1';  for i := 0 to n-1 do    result := result+'0';end; limit is 10 ** High(integer)-1 For systems with true 80 bit extended: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 operator ** (const a,b:extended):extended;begin  Result := round(Exp(ln(a) * b));// btw this is standard for power.end; So for win32 this obtains correct results, but on win64 you get the same error because extended is a double. It is also only valid in the extended domain. ---------------------------------------------------------------------- Paolo · 2024-07-02 09:15 But QuoteIf you replace every int64... You miss the fact that powerint has to manage negative bases. ---------------------------------------------------------------------- Thaddy · 2024-07-02 15:59 It doesn't. And it should not. Not if the negative base is an overflow. As explained the negative is simply caused by overflow. Not a base. Btw: you can predict what 10 ** 19 is using just your mind, you don't need a computer for that. That is the reason I added the string example. ---------------------------------------------------------------------- Paolo · 2024-07-02 18:30 sooner or later a float/integer/etc... number overflow Jamie code handle correctly negative bases with integer index 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  IntPow(-2,3) //that is -8   this one no! (and why there is a round ?) 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  operator ** (const a,b:extended):extended;begin  Result := round(Exp(ln(a) * b));   // btw this is standard for power.end;   if you loook at math unit infact there is a branch to handle negative bases only in the case of integer exponent (otherwise it has not sense) 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  function power(base,exponent : float) : float;  begin    if Exponent=0.0 then    result:=1.0    else if (base=0.0) and (exponent>0.0) then      result:=0.0    else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then //<-- here selection of Powerint       result:=intpower(base,trunc(exponent))                                //<-- here negative bases can be hadled    else      result:=exp(exponent * ln (base));  end; ---------------------------------------------------------------------- Thaddy · 2024-07-02 19:47 There is only a round to get a integer result, which is what he asked. Don't ask. I am already angry of all your math teachers. Are they still alive? Hey, it is 10 to a power of ... any integer, just count the zero's.... That is, put it mildy, obvious? ---------------------------------------------------------------------- tetrastes · 2024-07-02 20:22 Quote from: Zvoni on July 01, 2024, 02:44:09 pmIt was more to point out: Where in blazes does the Statement "It uses the Double-Version" comes from? In my quote from the Assembler window i clearly see "intpower" and "longint", though no idea what the "extended" is supposed to mean. Don't you clearly see fildll, fstpt, fistpll, fwait? These are x87 instructions. As for MATH_$$_INTPOWER$EXTENDED$LONGINT$$EXTENDED, this is a function with 2 arguments: extended and longint, returning extended. This asm code is for x87. At x86_64-win64 platform, where there is no 80-bit extended, produced asm code uses SSE2 instructions by default and MATH_$$_INTPOWER$DOUBLE$LONGINT$$DOUBLE. ---------------------------------------------------------------------- Paolo · 2024-07-02 22:24 what I see is Code: [Select]function Round(d: Extended):Int64; so a function like Code: [Select]function Power(a, b: Extended):extended; begin   result:=round(Exp(ln(a) * b)); end; should have the same problem, because Round gives a Int64 that cannot be higher than 10^18. Am I right ? moreover as you said you have 10^18=1000000000000000001 that is wrong, using math intpower I see the correct value of 1E18 ! any comments ? all the above is without controversy at least from my side Here Win64. ---------------------------------------------------------------------- jamie · 2024-07-02 22:31 I find it odd that a INTPOWER function returns a Float and not an integer? On top of that, the existing code int the math for this looks like spaghetti. I remember running into this before and ended using something simpler. 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 Function IntPower(a,b:Int64):Int64;begin result := a; If A =0 Then exit; Result := trunc(Exp(ln(abs(a)) * b));end;                                      This is faster than a pure INT version in a large loop, but it will generate a fault if you over do it, at least it returns an integer. ---------------------------------------------------------------------- Paolo · 2024-07-02 22:37 But here intpower means that the exponent is an integer, not the type of of the result. Having float you have a wider range of values. ---------------------------------------------------------------------- jamie · 2024-07-02 22:56 Maybe but I like the Int64 results. ---------------------------------------------------------------------- Thaddy · 2024-07-03 05:55 Quote from: Paolo on July 03, 2024, 12:24:59 amshould have the same problem, because Round gives a Int64 that cannot be higher than 10^18. Am I right ? Yes, you are. I provided proof of that. I also provided proof that, while the integer UNSIGNED solution - you provided as signed - and I adapted can express 10**19, but not 10**20 It is simply the limit of a qword. To express higher numbers you need a bignumber library. (or E notation and keep it in the float domain, using formatfloat and the likes for display) It is a bit the same as with permutations running out of space from another posting at the same limit. Focus on high(qword) to see what causes this. It has twice the space of a signed int, but it bombs therefor on  the next one, since that requires... twice the space, not physical space, but computational space. Anyway powers on base 10 can be done by simply adding zero's, hence the string version. 10**X has an extra X-2 zero's. So you do not really need to compute it.