### Truncating a float to a specified number of decimal digits

Pages: 12
> You're trying hard, I'll give you that,

I don't have to try hard to recognise palooka code when I see it.
closed account (48T7M4Gy)
.
Last edited on
lastchances code is fine; it is for one specific, clearly stated, value of x:
 ``123`` ``````double x = 1234.5678; x = (int)( 100 * x ) / 100.0;``````

Your code, which foolishly tries to generalise it to all possible values that x can take, "well within the 14 or so significant digits represented by a double" is the palooka code.

 Q: if we want to truncate (rather than round) a float to a specified number of digits, how can we do so? Palooka answer: Exactly as @lastchance wrote x = (int)( 100 * x ) / 100.0; and as he demonstrated by 'upping' the precision you are well within the 14 or so significant digits represented by a double.
I haven't seen nonsense like this for ages. Kemort throws toys out of pram, deletes all posts, reports all JLBorges' posts (I presume it was Kemort doing that; seems to fit) and then changes name to JIBorges.

Just like the old days around here. Fortunately, we can trust JLBorges to act like an adult and just ignore it.
Last edited on
closed account (48T7M4Gy)
JUST SO EVERYBOY KNOWS HOW I FEEL BY THIS BULLY CHIMING IN I SENT THE FOLLOWING PM TO MOSCHOPS:
 Why don't you mind your own business. You don't know how hurtful your and Borges comments are. I have done nothing here to be anything but constructive and despicable characters like you and him and a few others can't bear it. You are a bully and one day will cause a great deal of harm to someone. You might even kill someone. You should be ashamed of yourself taking sides and self righteously taking sides and ganging up. You are a disgrace as human being. Shame on you. I dare you to behave like a human instead of being a gutless parasite. Shame on you. You horrible person who has no insight into the consequences of your venomous and cowardly smug behaviour.

Last edited on
closed account (48T7M4Gy)
ITS INTERESTING MOSCHOPS HASN'T EVEN GOT THE COURAGE TO REPLY.

Sociopaths always scurry for cover when they're exposed.

Report me you freak. That's all you're made of - weak cowardice. You scurrilous freak, not even courage enough to defend your dispicable behavior.
 Sociopaths always scurry for cover when they're exposed.

No they don't. Sociopaths are charming manipulators. They deal with being exposed by manipulating the people around them, or violence.
closed account (48T7M4Gy)
 No they don't.

Yes they do!

And what we see here is a couple of very nasty and mean-spirited cowards who delight in taking the violence option.

He's right, it's occurred in the past here well before your 175 posts and these few (4 or 5) criminals have tried to bully many many people. They are a very destructive influence that turns a lot of beginners away.

I'm one of the few who has been prepared to call them out and that's in response to their criminal actions not my provocation.

You're playing a potentially very serious game that you might not like to bear the consequences of by chiming in on something that is deeper than you can imagine.
If the implementation uses IEEE floating point (most do), using std::modf() which yields exact values would make the (robust) code simpler.

 ``12345678910111213`` ``````double trunc_n( double value, std::size_t digits_after_decimal = 0 ) { if( digits_after_decimal >= std::numeric_limits::digits10 ) return value ; double integral_part ; const double fractional_part = std::modf( value, std::addressof(integral_part) ); std::intmax_t multiplier = 1 ; while( digits_after_decimal-- ) multiplier *= 10 ; return integral_part + double( std::intmax_t(fractional_part*multiplier) ) / multiplier ; // or: return integral_part + std::trunc(fractional_part*multiplier) / multiplier ; }``````

http://coliru.stacked-crooked.com/a/4320f7f8196596dd
Comedy gold.
Topic archived. No new replies allowed.
Pages: 12