Forum > General
Round in 32 bit
KonradLaz:
why round in 64 bit mode for curr = 658125000 gives 658130000 and for 32 bit mode 658120000 ?
dseligo:
I get 65812 both under 32 and 64 bit.
Maybe you should give more detail: what processor are you compiling to, what OS, FPC version, test program you use, ...
jamie:
Hmm lets see.
Extended in 32 bit mode gives you a better round.
Extended in 64 bit mode gives you a worse round because its really a DOUBLE.
Try Double in both maybe?
Thaddy:
In the case of currency it does not matter because the round is correct to 0.00005.
Currency is not a float, but a scaled integer.
This is not immediately obvious but you can see that from the range of comp:
https://www.freepascal.org/docs-html/ref/refsu5.html or from e.g. the Delphi documentation:
"You can use for financial calculations the Currency type. The Currency type is in essence an integer scaled by 10000 (this value allows exact division by 10). You can store four decimal digits in a Currency variable, anything that goes beyond this limit is rounded."
Which is the same in fpc;
https://www.freepascal.org/docs-html/3.0.0/ref/refsu6.html
"The currency type is a fixed-point real data type which is internally used as an 64-bit integer type (automatically scaled with a factor 10000), this minimalizes rounding errors."
So there is no round error at all, unless you perform calculations with real floats and you should avoid that.
KonradLaz:
I found something like this in the documentation:
Description
Round
rounds
X
to the closest integer, which may be bigger or smaller than
X
.
In the case of
.5
, the algorithm uses "banker's rounding":
.5
values are always rounded towards the even number.
ok I wrote my own rounding function in accordance with the tax interpretation
function TaxRound(Value: Double): Integer;
begin
if Frac(Value) = 0.5 then
Result := Trunc(Value) + 1
else
Result := Round(Value);
end;
Navigation
[0] Message Index
[#] Next page