Forum > Beginners

floating point literals

(1/7) > >>

srvaldez:
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?

KodeZwerg:
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.

srvaldez:
I knew that you could use a cast, but having to do that for this simple case is lame

KodeZwerg:

--- Quote from: srvaldez 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
--- End quote ---
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.

srvaldez:
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

Navigation

[0] Message Index

[#] Next page

Go to full version