floor(positive double) vs trunc(positive double) different

H

Hicham Mouline

If we consider a positive double argument, that is not 0, not NaN not
infinity.

do floor() and trunc() return the same value then?

Regards,
 
B

Barry Schwarz

If we consider a positive double argument, that is not 0, not NaN not
infinity.

do floor() and trunc() return the same value then?

floor() rounds down. trunc() rounds to 0. For positive values, and
0, the results are the same.
 
K

Keith Thompson

none said:
<snip part that made me laugh>

I think they have to return the exact same value under said circumstances.

C99 7.12.9.2 The floor functions p2

"The floor functions compute the largest integer value not greater than x."

C99 7.12.9.8 The trunc functions p2

"The trunc functions round their argument to the integer value,
**in floating format** [emphasis added], nearest to but no larger
in magnitude than the argument."

Do both return a float like trunc(), or does floor() return an integer?

Both return double (not float).

Hint: The best way to answer simple questions like that
is to read the documentation. If you don't have something
like "man floor" and/or "man trunc" on your system, you can
always grab a copy of the latest draft of the C standard from
<http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top