Efficient way to drop the fractional part from float but retain the type

Efficient way to drop the fractional part from float but retain the type



I know that by std::round, std::floor or std::ceil or simply typecasting we can drop the decimal part, but with typecasting, the the variable loses its original type ( float or double ).



Is there any way, I can retain the type and still drop the fractional part efficiently.



One of the method I thought was subtracting the fractional part from the number, but that isnt so efficient. So, maybe there are some other methods?



Example -


float a = 123.456;

float b;

b = do something on a;



result b is 123.0





Why can't you cast to an int then back to a float and let the compiler deal with it?
– MeetTitan
Sep 1 at 8:34





@MeetTitan That's all kinds of trouble. The range of a float is much bigger than int.
– melpomene
Sep 1 at 8:39


float


int





@MeetTitan [conv.fpint] "A prvalue of a floating-point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type."
– melpomene
Sep 1 at 8:52


[conv.fpint]





b = (int)a; should do.
– Luis Colorado
Sep 3 at 9:07



b = (int)a;





@melpomene, not in granularity. If you take a 32bit floating point and try to assign it something like 1,000,000,123 to a 32bit integer lvalue, you probably get some surprise.... and that's well under the range of 32bit integer types. In my case I got 1,000,000,128, due to the lack of significand bits in the floating point, that are used in the integer counterpart. :)
– Luis Colorado
Sep 3 at 9:16



1,000,000,123


1,000,000,128




2 Answers
2



For example, with std::round original type is preserved:


std::round


float round(float arg);
double round(double arg);



It is also true for std::floor and std::ceil.


std::floor


std::ceil



Addition. Here are some benchmarking results.



Compilers: gcc 7.3.0 and msvs 2018 15.9.0, machine: Core i7-4770.


gcc 7.3.0


msvs 2018 15.9.0


Core i7-4770



Code. Compilation options:



Frankly speaking I don't think these results are very relevant (in the absolute values) on their own. With fast math option some functions reduce just to a single assembly instruction vroundss. The real code should be profiled to get relevant results.


vroundss





oh..that was simple.. Didnt know that round/floor/ceil retains the type. But makes sense.
– infoclogged
Sep 1 at 8:52





This is actually the "greater" answer than the accepted one, IMO.
– Rudy Velthuis
Sep 1 at 16:32





@RudyVelthuis Why? It doesn't answer the question in the title (how to drop the fractional part of a number).
– melpomene
Sep 1 at 16:35





@RudyVelthuis - well, the question was to efficiently drop the fractional part, coz the dataset where it will be used is huge. I did not benchmark, but I do strongly feel that trunc will win among the other contestants. Have changed ths subject line to be more specific.
– infoclogged
Sep 3 at 20:50






@infoclogged please don't change your question after it's been answered; if you have a new question, ask it as a new question.
– Sneftel
Sep 3 at 20:56



Check out modf:


modf


#include <cmath>

float a = 123.456;
float b, c;

c = std::modf(a, &b);
// c = 0.456
// b = 123.0



You don't have to use the return value (the fractional part) if you don't need it.



Or (since C++11) trunc:


trunc


float b = std::trunc(a);





great answer. Whats the difference of trunc and modf from round ( I now know, that round retains the type )?
– infoclogged
Sep 1 at 8:53






@infoclogged round rounds to the nearest integer (away from 0), so e.g. round(0.7) is 1.0, but trunc(0.7) is 0.0 (it just drops the fractional part).
– melpomene
Sep 1 at 8:55


round


round(0.7)


1.0


trunc(0.7)


0.0





it looks like trunc is really dumb and should be the fastest among all round, ceil, floor etc. this works for me.
– infoclogged
Sep 1 at 9:08





@infoclogged: trunc() is not "dumb". It takes some work to convert an FP value to an integer value, the same kind of work as, say, for floor() or ceil(), or even round().
– Rudy Velthuis
Sep 1 at 16:31






@melpemone: Which bits/how many bits? But you don't set some bits to 0. you must know how many bits can be removed, then you must round to the last remaining bit position and then you must normalize the result. That is a little more work than just cutting off some bits. But usually this is one in hardware, so no big deal.
– Rudy Velthuis
Sep 4 at 16:22




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)