Re: Decimal rounding function



Bubba <nickname@xxxxxxxxxxxxxxxxxxx> writes:
Greetings to all,

I'm searching for cheap, quick and easy rounding function in C, but (there
had to be but!) without any libraries. In other words, is there a sane
mathematical (or any other, for that matter) way that can be implemented
in C and round arbitrary number of decimal places them to desired decimal
places?

So, basically, something like this:

double round (double number, unsigned decimalPlaces) {
/* some magic here */ }

Basic point is to make it portable for everything from Motorola 68000 to
embedded ARM or latest gaming x64 CPU.

I tried doing it myself but couldn't even begin being puzzled about
determining number of decimal places in the first place, let alone doing
any rounding with them.

Also did some Googling research but found only solutions with strings and
similar tricks (which I was acquainted with) or written in C++. Appreciate
any help or hint.

(The name "round" is already taken.)

There's no such function in the standard, and few such functions
floating (ahem) around out there, because it's really not as useful
as you might think.

Rounding a floating-point to a specified number of decimal places is
useful only if you're producing a string representation of the number.
(Well, there might be *some* use for it, but I can't think of any.)
And if you're doing that, you might as well just produce the string
directly.

round(1.375, 1) should give you 1.4, but 1.4 cannot be represented
exactly in binary floating-point. You might get something like
1.399999999999999911182158029987476766109466552734375, which depending
on how you print it, might come out as "1.3".

What are you really trying to accomplish? What do you intended to
do with the rounded number once you've generated it? Why not just
keep it in full precision until you're ready to truncate it when
printing it or converting it to a string?

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Why (0.09+0.01-0.1) is not equal to 0.09+0.01-0.1 ?
    ... The binary to decimal conversion gives full accuracy by default, but also includes rounding algorithms since users will seldom care about hundreds of figures. ... Function D2BAs String ... the work involved in avoiding lost precision due to type conversion ... ' D2Dis sufficient to uniquely determine the binary representation of x ...
    (microsoft.public.excel)
  • Re: Why (0.09+0.01-0.1) is not equal to 0.09+0.01-0.1 ?
    ... > gives full accuracy by default, but also includes rounding algorithms ... > partitioned mantissa by without risk of overflow ... > Function D2BAs String ... the work involved in avoiding lost precision due to type ...
    (microsoft.public.excel)
  • Re: reformat of 17 tables from integer to single
    ... Sub AlterFieldType(TblName As String, FieldName As String, DataType As ... field size and decimal places but I dont see format. ... the default of DecimalPlaces is 2 ?? ...
    (comp.databases.ms-access)
  • Re: Issues with rounding
    ... There's a huge body of literature out there on BIGNUM algorithms including issues of ... Certainly you can round the string representation. ... the position of the rounding digit. ... nearest whole number multiple of this unit. ...
    (microsoft.public.vc.mfc)
  • Re: Engineering Format Number in VB6
    ... DecimalPlaces As Long = 1) As String ... Rick (MVP - Excel) ... to engineering format. ...
    (microsoft.public.vb.general.discussion)