Recent

Author Topic: Range check, converting extended to double  (Read 575 times)

Markus

  • New Member
  • *
  • Posts: 22
Range check, converting extended to double
« on: May 16, 2025, 11:59:44 am »
I just found an error in my code where the program wrote to an item 0 of an array that was defined from 1 upwards. This produced a strange behavior where another variable was changed unintentionally. I assumed that an error message should have appeared, but it did not happen.

Someone wrote that such problems can be avoided by activating:
Project / Project Options / Compiler Option / Debugging / Checks / Range.

After activating this option (-Cr) to avoid such programming errors in the future, I get other problems, for instance when passing a extended variable to a function that uses TDateTime (=double) as parameter, which was accepted before.

1) Is there a way to only get error messages when an array index is out of range?

2) Is there a way to convert extended -> double (reduce precision to double)

jamie

  • Hero Member
  • *****
  • Posts: 6964
Re: Range check, converting extended to double
« Reply #1 on: May 16, 2025, 12:06:42 pm »
Use range check, its in the compiler options of your project options

As for extended. Dont use it?

Btw, welcome to real hard core coding
The only true wisdom is knowing you know nothing

Markus

  • New Member
  • *
  • Posts: 22
Re: Range check, converting extended to double
« Reply #2 on: May 16, 2025, 12:51:17 pm »
Don't use it? Nice idea.. :D

The problem is that it is part of a function unit which I use in about 10 other projects. Changing that function could have unwanted effects on the other projects.

Zvoni

  • Hero Member
  • *****
  • Posts: 2982
Re: Range check, converting extended to double
« Reply #3 on: May 16, 2025, 12:58:24 pm »
Don't use it? Nice idea.. :D

The problem is that it is part of a function unit which I use in about 10 other projects. Changing that function could have unwanted effects on the other projects.
Use overloads?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Paolo

  • Hero Member
  • *****
  • Posts: 584
Re: Range check, converting extended to double
« Reply #4 on: May 16, 2025, 01:28:14 pm »
What OS ? On win64 extended = double. In general the jamie advice is right, do not use extended that could be not available everywhere.

Markus

  • New Member
  • *
  • Posts: 22
Re: Range check, converting extended to double
« Reply #5 on: May 16, 2025, 02:29:33 pm »
I am using Windows 10, 64-bit. I get a runtime error with this compiler option (-Cr) switched on when calling a special seldom used function with an extended variable which is actually defined to get TDateTime as input parameter. The variable value is nearly zero in that case (something with a large negative exponent).

I will check if extended could be replaced by double. The original idea was that some often used procedures which I have in a unit used in all projects have the parameter extended to be able to be called by all kinds of real values. Maybe this was a wrong idea..

tetrastes

  • Hero Member
  • *****
  • Posts: 648
Re: Range check, converting extended to double
« Reply #6 on: May 16, 2025, 02:42:33 pm »
After activating this option (-Cr) to avoid such programming errors in the future, I get other problems, for instance when passing a extended variable to a function that uses TDateTime (=double) as parameter, which was accepted before.
Just because it was accepted doesn't mean it worked properly. You have to find the cause of these problems, as f.e. DateTimeToStr(extended_var) works without problem (at i386-win32 with "true" 80-bit extended).

2) Is there a way to convert extended -> double (reduce precision to double)
double_var := extended_var
double(extended_var) works also, except in $MODE DELPHI (fpc 3.2.2)

I am using Windows 10, 64-bit.
But what is bitness of your programs?



 

TinyPortal © 2005-2018