Recent

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

user5

  • Sr. Member
  • ****
  • Posts: 357
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

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
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
keep it simple

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
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 »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

wp

  • Hero Member
  • *****
  • Posts: 11916
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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
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.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
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.
keep it simple

user5

  • Sr. Member
  • ****
  • Posts: 357
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: 3197
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

 

TinyPortal © 2005-2018