Int returns the integer part of any Real X, as a Real.What you are looking for is the trunc function: https://www.freepascal.org/docs-html/rtl/system/trunc.html
Trunc returns the integer part of X, which is always smaller than (or equal to) X in absolute value.
With the exception of Div and Mod, which accept only integer expressions as operands, all operators accept real and integer expressions as operands.The reason div is not overloaded for doubles, is very simply because it's the integer division it's an operation thats solely for dividing integers by integers and returning integers.
Just modify the first message in your thread; as well as the body of the message, you'll also be able to amend the title where you can add [SOLVED] as a prefix or suffix.
What I do is...
i := round(int(r));
trunc is not good because of negative numbers.
Result:
iter = 10000000;For n operations the rounding error of nearest even rounding is about 0, while for rounding up it is around -n/2 and for rounding down it's around n/2.
Round up error: -498955,69999671
What do you mean?
n(iter) is 107, so ~n/20 ;)
Please forgive my ignorance as I did not follow this topic but SetRoundMode (https://docs.freepascal.org/docs-html/3.0.0/rtl/math/setroundmode.html) ?
But in most cases one needs rmNearest, and so deals with banker's rounding. And while it's good to count money (as it follows from its name), for many mathematical and physical tasks it's not correct.I mean there is no "correct way" for rounding, rounding by definition is creating an error. The question is just what kind of error you want to minimize. Nearest Even rounding minimizes the commulative error. And it's not just for counting money, it's whenever you perform multiple operations and are interested in keeping the commulative error as low as possible. For example if you do a finite difference time domain simulation in physics, where you simulate in small time steps, and rounding is performed on each step, the result of which is based on the rounded result from the first step, you also want to have the rounding error to be as small as possible.
I mean there is no "correct way" for rounding, rounding by definition is creating an error.
You are absolutely right in this. But it's hard to say anything without seeing formulae. And there is mathematical rule for .5 rounding, and it's not "banker's". I suspect that fpc's choice (following Delphi?) reflects the field which fpc mainly aimed to.Has very little to do with Delphi, but it's simply the default as specified in the IEEE 754 standard for floating point operations. The reason Fortran doesn't do it this way is probably just because Frotran predates this standard. But Every language that has been implemented since then usually follows the IEEE 754 standard. And Backwards compatibility is king. You can also see this in C, where the original round function that is part of the language since the beginning rounds away from zero, but later the standard introduced a new function rint, which is compliant with IEEE 754.
Has very little to do with Delphi, but it's simply the default as specified in the IEEE 754 standard for floating point operations. The reason Fortran doesn't do it this way is probably just because Frotran predates this standard.But standard defines 5 modes. And one of them "Round to nearest, ties away from zero". Why TFPURoundingMode has only 4 modes?
Also as I said it's the default configuration of the FPU. While you can change the FPU mode, I won't advise for doing it, because this changes the behavior for everything not just that one operation in your code. So enforcing a different behavior just for your code requires emulating it in software, which is for one more difficult to implement, but more importantly also slower.You mean using SetRoundMode? If this is so, why it exists at all?
Why doesn’t the 𝚒𝚗𝚝 function return an integer value?I have not really read the answer in this thread:
Simply use
N := Trunc(X + 0.5);
But if you really want it, you can always just link against libc and use their round function. it's also fairly optimized and only like 10%-20% slower than the bare metal rint function
round(x)
From the expression x that shall be of real-type, this function shall return a result of integer-type.
If x is positive or zero, round(x) shall be equivalent to trunc(x+0.5); otherwise, round(x)
shall be equivalent to trunc(x-0.5).