Not Yet Answered # rounding function with precision

Nick Evan 4,005 Narue 5,707 ArkM 1,090 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

If you're using a C-compiler, it might come from <math.h>. I'm not sure of this, but I vaguely remember that there's a round() function in there somewhere.

To my knowledge, there is no round()-function in C++, but I could very well be corrected in the next few replies.

0

>I vaguely remember that there's a round() function in there somewhere.

C99 added a bunch of stuff, including a round function. However, the signature doesn't match what the OP is talking about, so I'd guess it's a compiler extension.

>i was wondering if someone knows what library it comes from

Check the compiler documentation. The function you've specified doesn't exist in either the C or C++ standard.

0

What's a funny thing - decimal rounding of binary floats...

May be it stands in good steads ;):

```
/**
* 2008-10-01 Beta version. No warranties...
* Rounding functions freeware mini-package.
* About rounding forms and MS rounding stuff
* see http://support.microsoft.com/kb/196652
* Dependencies: <math.h> <float.h>
* Languages: standard C and C++
* Not optimized!..
*/
/** Not-optimized 10 power n (n > 0) */
static double tenpow(int n) {
double y = 10.0;
while (--n > 0)
y *= 10.0;
return y;
}
/** 64-bit double precision ~ 15 digits. */
static int nMax = DBL_DIG; /* from float.h */
/** Round off with Banker's method, n in -15..15 */
double Round(double x, int n) {
bool neg = (x < 0.0);
double ipart, fpart;
double y, p;
int id;
if (neg)
x = -x;
if (n > 0) {
double yy;
fpart = modf(x,&ipart);
if (n > nMax)
n = nMax;
p = tenpow(n);
y = fpart * p;
fpart = modf(y,&yy);
if (fpart < 0.5)
fpart = 0.0;
else if (fpart > 0.5)
fpart = 1.0;
else { /* Banker's Method */
id = (int)fmod(yy,10.0);
fpart = (id&1)? 1.0: 0.0;
}
yy += fpart;
y = ipart + yy / p;
}
else if (n < 0) {
if (n < nMax)
n = -nMax;
p = tenpow(-n);
y = x / p;
y = Round(y,0) * p;
}
else { /* n == 0 */
fpart = modf(x,&ipart);
if (fpart > 0.5)
ipart += 1.0;
else if (fpart < 0.5)
;
else { /* Banker's Method */
id = (int)fmod(ipart,10.0);
if ((id&1) != 0)
ipart += 1.0;
}
y = ipart;
}
return neg?-y:y;
}
/** Symmetric arithmetic rounding, n in -15..15 */
double round(double x, int n) {
bool neg = (x < 0.0);
double ipart, fpart;
double y, p;
int id;
if (neg)
x = -x;
if (n > 0) {
double yy;
fpart = modf(x,&ipart);
if (n > nMax)
n = nMax;
p = tenpow(n);
y = fpart * p;
fpart = modf(y,&yy);
if (fpart < 0.5)
yy += 1.0;
y = ipart + yy / p;
}
else if (n < 0) {
if (n < -nMax)
n = -nMax;
p = tenpow(-n);
y = x / p;
y = round(y,0) * p;
}
else { /* n == 0 */
fpart = modf(x,&ipart);
y = (fpart < 0.5)? ipart: ipart + 1;
}
return neg?-y:y;
}
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...