Recent

Author Topic: Invalid floating point operation  (Read 513 times)

user5

  • Full Member
  • ***
  • Posts: 246
Invalid floating point operation
« on: August 12, 2019, 07:55:52 am »
Can Trunc cause an invalid floating point error if its used on a very long decimal number?
Will the try statement catch an invalid floating point error?

Munair

  • Sr. Member
  • ****
  • Posts: 474
  • Try to KISS (keep it simple, smart)
    • Ditrianum
Re: Invalid floating point operation
« Reply #1 on: August 12, 2019, 08:18:39 am »
Does the DecimalSeparator match? The other day I had the same error reading large floating numbers. the system's decimal separator was set to a comma, while the data used a point. The following line solved my problem:
Code: Pascal  [Select]
  1. DefaultFormatSettings.DecimalSeparator := '.'; // overwrite system's default
Lazarus 2.0.4; Manjaro, Debian, Windows

Thaddy

  • Hero Member
  • *****
  • Posts: 9292
Re: Invalid floating point operation
« Reply #2 on: August 12, 2019, 08:24:38 am »
https://www.freepascal.org/docs-html/rtl/system/trunc.html
So they are bound to output by the limits of an int64 and an input limit of the largest available floating point type on the architecture the program runs on.

But note, indeed a wrong decimal separator can also cause it.
« Last Edit: August 12, 2019, 08:31:49 am by Thaddy »
also related to equus asinus.

wp

  • Hero Member
  • *****
  • Posts: 6483
Re: Invalid floating point operation
« Reply #3 on: August 12, 2019, 09:26:39 am »
This question is about an issue of the trunc function, not about string-to-float conversion. I cannot imagine how the decimal separator can have any effect on the trunc function. Please convince me with an example.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Thaddy

  • Hero Member
  • *****
  • Posts: 9292
Re: Invalid floating point operation
« Reply #4 on: August 12, 2019, 11:59:52 am »
This question is about an issue of the trunc function, not about string-to-float conversion. I cannot imagine how the decimal separator can have any effect on the trunc function. Please convince me with an example.
In the case the values are read from a file in string format. Not enough information, really.
also related to equus asinus.

Munair

  • Sr. Member
  • ****
  • Posts: 474
  • Try to KISS (keep it simple, smart)
    • Ditrianum
Re: Invalid floating point operation
« Reply #5 on: August 12, 2019, 12:46:57 pm »
Indeed, not enough information. It would help if the OP provided some code. But Trunc(StrToFloat(..)) could definitely cause the aforementioned issue.
Lazarus 2.0.4; Manjaro, Debian, Windows

user5

  • Full Member
  • ***
  • Posts: 246
Re: Invalid floating point operation
« Reply #6 on: August 12, 2019, 08:51:27 pm »
    Thanks for your responses. What I had in mind for the Trunc issue was something like: Trunc(newext1), with newext1 being an extended number.
    By the way, will the try statement catch an invalid floating point error? I can't seem to find out from the documentation.

winni

  • Hero Member
  • *****
  • Posts: 591
Re: Invalid floating point operation
« Reply #7 on: August 12, 2019, 11:27:29 pm »
 Hi!
There were problems in earlier versions catching division by zero, but now it should work.
Test this:

Code: Pascal  [Select]
  1.      
  2.        try
  3.         x := 100 / 0;
  4.         except
  5.           on EDivByZero do
  6.             ShowMessage('Division by zero!')
  7.         end;


Winni