Recent

Author Topic: floating point literals  (Read 2940 times)

srvaldez

  • New Member
  • *
  • Posts: 36
floating point literals
« on: February 20, 2024, 02:24:29 pm »
if x is declared as double and then assigned 1.0/3.0 the value assigned is single precision, however if 1/3 is assigned then the precision is double
for a beginner this doesn't make sense and can possibly lead to wrong results, are there post-fix operators or symbols that specify the literal precision?
for example in basic you have the # symbol, so in basic to ensure that double precision is meant you could write: x=1#/3#
is there something similar in free pascal?
« Last Edit: February 20, 2024, 02:26:38 pm by srvaldez »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2241
  • Fifty shades of code.
    • Delphi & FreePascal
Re: floating point literals
« Reply #1 on: February 20, 2024, 02:28:46 pm »
Use proper type to get desired type."Double(1.0) / Double(3.0)" should return you a Double. "Double(1.0 / 3.0)" i could imagine works too.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

srvaldez

  • New Member
  • *
  • Posts: 36
Re: floating point literals
« Reply #2 on: February 20, 2024, 02:33:58 pm »
I knew that you could use a cast, but having to do that for this simple case is lame

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2241
  • Fifty shades of code.
    • Delphi & FreePascal
Re: floating point literals
« Reply #3 on: February 20, 2024, 02:44:02 pm »
I knew that you could use a cast, but having to do that for this simple case is lame
What kind of type is "1.0" from your point of view?
From my point of view it is first of all a float and there it begins with the "Single" type, "1.0" does fit into "Single" so compiler does treat it as such.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

srvaldez

  • New Member
  • *
  • Posts: 36
Re: floating point literals
« Reply #4 on: February 20, 2024, 02:48:09 pm »
in this case the compiler should be smart enough to treat the literals as double because the variable on the left is of type double

Zvoni

  • Hero Member
  • *****
  • Posts: 2364
Re: floating point literals
« Reply #5 on: February 20, 2024, 03:46:14 pm »
in this case the compiler should be smart enough to treat the literals as double because the variable on the left is of type double
No.
Afaik, every programming language goes bottom-up from the smallest Type the literal value fits in, irrespective what's left hand side of the equal-symbol.
The same with "evaluating" if it's a mathematical calculation: It takes the smallest fitting Datatype the result fits in, AND THEN checks if it fit's in/is compatibel to the Datatype left hand side.

The only way is to "hardcast" right hand side, and IIRC, at least one operand should be enough
kinda
Code: Pascal  [Select][+][-]
  1. Var d:Double;
  2. d:= Double(1.0)/3.0;
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

srvaldez

  • New Member
  • *
  • Posts: 36
Re: floating point literals
« Reply #6 on: February 20, 2024, 03:55:26 pm »
my question has not been answered, is there a post-fix symbol for number literals?
in C, 1.0 is treated as double, 1.0f is treated as single and similarly for integer types, L for signed 32-bit, UL for unsigned 32-bit and LL and ULL for 64-bit integers

explainedd

  • New Member
  • *
  • Posts: 11
Re: floating point literals
« Reply #7 on: February 20, 2024, 03:56:07 pm »
Unfortunately no typed floating-point literals in Pascal.
The compiler is "smart" and chooses the type with the lowest bit width, where the value is representable.

You can apply the -CF64 compiler option to parse every floating-point literal as 64bit double.

Here is a more detailed explanation:
https://wiki.freepascal.org/IEEE_754_formats

srvaldez

  • New Member
  • *
  • Posts: 36
Re: floating point literals
« Reply #8 on: February 20, 2024, 03:59:11 pm »
thanks explainedd  :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14615
  • Sensorship about opinions does not belong here.
Re: floating point literals
« Reply #9 on: February 20, 2024, 04:16:16 pm »
thanks explainedd  :)
Or {$MINFPCONSTPREC 64} in your source.
« Last Edit: February 20, 2024, 04:24:40 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

srvaldez

  • New Member
  • *
  • Posts: 36
Re: floating point literals
« Reply #10 on: February 20, 2024, 04:29:12 pm »
thanks Thaddy  :)
I like it better in the source, just in case you want to share code

srvaldez

  • New Member
  • *
  • Posts: 36
Re: floating point literals
« Reply #11 on: February 20, 2024, 04:36:20 pm »
is there a place to share a work-in-progress code?
I know there's the third party announcement section but my code is beginner type code with a bit of advanced stuff mixed-in

Thaddy

  • Hero Member
  • *****
  • Posts: 14615
  • Sensorship about opinions does not belong here.
Re: floating point literals
« Reply #12 on: February 20, 2024, 04:40:52 pm »
is there a place to share a work-in-progress code?
I know there's the third party announcement section but my code is beginner type code with a bit of advanced stuff mixed-in
zip it and attach is enough.
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Bart

  • Hero Member
  • *****
  • Posts: 5297
    • Bart en Mariska's Webstek
Re: floating point literals
« Reply #13 on: February 20, 2024, 04:49:18 pm »
There is already a lenghty discussion about this on fpc ML.
No need to duplicate this here.

Bart

nanobit

  • Full Member
  • ***
  • Posts: 162
Re: floating point literals
« Reply #14 on: February 20, 2024, 05:15:04 pm »
I knew that you could use a cast, but having to do that for this simple case is lame

Besides of operand types, expressions also have operation precision.
Since FPC3.2,  {$excessprecision on} is supposed to maximize precision of operations.
But there are known bugs about this, for example:
(1.0/5), (1/5.0) are less precise than (1.0/5.0), (1/5).

And if single-type operands can represent certain exact values,
then larger precision in operations can be still appropriate (depends on intentions).

 

TinyPortal © 2005-2018