How to make truncation in C++
-
What function? Just cast it to an integer. Or if you want a function:
__inline int trunc(float f)
{
return (int)f;
}It is a crappy thing, but it's life -^ Carlo Pallini
-
Presuming you want similar behaviour for numbers < 0
trunc(-7.1) = -7;
trunc(-7.6) = -8;then this should work
int trunc(double d) { return floor(d+0.5); }
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
If I do it in this way, the results are:
trunc(7.1) = 7;
trunc(7.6) = 7; // fractional part is just droppedOh sorry, I overlooked your previous post. You can use one of those flooring or ceiling functions. [Added] OK, Stuart just replied with a floor function at the same time. [/added]
It is a crappy thing, but it's life -^ Carlo Pallini
-
rounded_num = (int)(num + (num < 0 ? -0.5 : 0.5));
-
You call it
trunc()
, but that is the expected behaviour ofround()
function. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You call it
trunc()
, but that is the expected behaviour ofround()
function. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I was guessing that the OP is trying to implement the functionality himself.
It is a crappy thing, but it's life -^ Carlo Pallini
-
I was guessing that the OP is trying to implement the functionality himself.
It is a crappy thing, but it's life -^ Carlo Pallini
Your guess is right, I suppose. However, OP must not give, even to his functions, deceiving names, unless, of course, he is a Klingon programmer. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
The FPU already does this for you.
//! Round double to integer inline int iround(double v){ int retval; \_\_asm fld qword ptr \[v\] \_\_asm fistp dword ptr \[retval\] return retval; } //! Round double to unsigned integer inline unsigned int uround(double v){ unsigned retval; \_\_asm fld qword ptr \[v\] \_\_asm fistp dword ptr \[retval\] return retval; }
-
The FPU already does this for you.
//! Round double to integer inline int iround(double v){ int retval; \_\_asm fld qword ptr \[v\] \_\_asm fistp dword ptr \[retval\] return retval; } //! Round double to unsigned integer inline unsigned int uround(double v){ unsigned retval; \_\_asm fld qword ptr \[v\] \_\_asm fistp dword ptr \[retval\] return retval; }
Nice.