# ROUND function source for IEEE-754

#### Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

•  Subject
• Author
• Posted on
Hello guys,

Where can I find source code for the function or macro
ROUND(number,precision) for IEEE-754 numbers?
( I'm using a 8051 processor)

Thanks,
Gerard

Re: ROUND function source for IEEE-754

Re: ROUND function source for IEEE-754

Assuming you are talking about C, from N869:

7.12.9.6  The round functions

Synopsis

[#1]
#include <math.h>
double round(double x);
float roundf(float x);
long double roundl(long double x);

Description

[#2] The round functions round their argument to the nearest
integer  value  in  floating-point  format, rounding halfway
cases away from zero, regardless  of  the  current  rounding
direction.

Returns

[#3] The round functions return the rounded integer value.

You can build your ROUND function from this with some
multiplications and divisions.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: ROUND function source for IEEE-754

This function will round a float to the precision given by 'precision'. If
precision is 2 then 525.345 will give 525.35 and with a precision of -2,
ROUND( 9856.0 , -2) will give 9900.

I already found the code but it's a lot of div. and mul.. I was hoping that
there is a smart way to do it when the number format ( such as IEEE-754)  is
known.

Gerard

Re: ROUND function source for IEEE-754

that
is

The IEEE format is a binary format. There is no speedy shortcut to decimal
rounding of binary fractions. Even as simple as 0.1 decimal is an unending
fraction in binary (0.0001100110011001100...).

Would you please tell what for are you needing the rounding?

For conversion to human-readable decimal output, it's IMHO easier to first
convert to floating-point decimal and then round it.

Tauno Voipio
tauno voipio @ iki fi

Re: ROUND function source for IEEE-754
Tauno,

I'm now using the floating-point decimal conversion and then round it, like
you suggest. I'm probably looking for something that there isn't.

Thanks!
Gerard

Re: ROUND function source for IEEE-754

If you had had the courtesy to quote something to indicate to what
you were replying, you would have also seen the following:

together with a description of the round() function.

An elementary consideration of the operations required, and the
internal data formats, would show you that such short cuts are not
available.  If ISO C adds the proposed decimal floating point
formats, that situation will change.  Don't hold your breath for
it.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: ROUND function source for IEEE-754
It's no matter of courtesy, I personally find it easier to follow a thread
without a lot of repeating quotes.

Nevertheless thanks.

Gerard

Re: ROUND function source for IEEE-754

I'm glad for you.  Of course we are all here purely to serve you.
Just let us know your demands in future, and watch the response.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: ROUND function source for IEEE-754
Have a break!! (or two)

Gerard