ROUND function source for IEEE-754

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

Reply to
Gerard Zagema
Loading thread data ...

already solved

Reply to
Gerard Zagema

Assuming you are talking about C, from N869:

7.12.9.6 The round functions

Synopsis

[#1] #include 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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

No, I'm talking about ROUND(number,precision).

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

Reply to
Gerard Zagema

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

Reply to
Tauno Voipio

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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

It's no matter of courtesy, I personally find it easier to follow a thread without a lot of repeating quotes.

Nevertheless thanks.

Gerard

Reply to
Gerard Zagema

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

Reply to
Gerard Zagema

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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

Have a break!! (or two)

Gerard

Reply to
Gerard Zagema

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.