# Thread: Rounding floats and sprintf()...

1. ## Rounding floats and sprintf()...

User (christopher.j.meisenzahl@citicorp.com.nospam) posted:

I apologize in advance, I know this topic is not new. But keep seeing large
functions to round floating point numbers.

Is anyone aware of a reliability or consistency problem with simply using
sprintf()?

Christopher

Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com

2. ## Re: Rounding floats and sprintf()...

sprintf is inconsistent with how it handles "5", such as whether it would round 1.335 to 1.33 or 1.34, probably due to the vagaries of floating point arithmetic. If it is not important for your test which way it rounds in such a situation, then sprintf is fine. If it matters, you'll probably want to use another rounding function instead.

------------------
Charles Reace

charles{DOT}reace{AT}verizon{DOT}net

3. ## Re: Rounding floats and sprintf()...

User Franzen, SPS PROFI, DA (C.Franzen@DeutschePost.de.nospam) posted:

Well -- just to give you another example, here is the routine we use to compare floating point values:

public function Min (
in Val1,
in Val2
)
{
if (Val1 < Val2)
return (Val1) ;
else
return (Val2) ;
}

public function Max (
in Val1,
in Val2
)
{
if (Val1 > Val2)
return (Val1) ;
else
return (Val2) ;
}

public function CompareFloat (
# Vergleicht 2 Dezimalzahlen Val1 und Val2 auf Decimals Nachkommastellen genau.
# Wird Decimals nicht angegeben oder enthält den Leerstring, wird auf 2 Nachkommastellen verglichen.
# Rückgabewerte; 0 -> Op1 == Op2
# -1 -> Op1 < Op2
# 1 -> Op1 > Op2
in Val1,
in Val2,
in Decimals
)
{
auto Multiplier ;
auto Exponent ;
auto Diff ;

if (nargs() == 2 &#0124; &#0124; Decimals=="")
Decimals = 2;
Multiplier = 1 ;
for (Exponent = 0; Exponent <= Decimals; Exponent++)
Multiplier *= 10 ;
Diff=Max(Val1,Val2)-Min(Val1,Val2) ;
if (Diff<(1/Multiplier))
return 0 ;
else if (Val1 < Val2)
return -1 ;
else
return 1;
}

I.e. we base all stuff like IsGreaterThan or IsEqual on that complex CompareFloat primitive. There is a real mess in WR (at least in 6.x) regarding floatiung point. Unfortunately. So the usual Round=Int (X+0.5) does SOMETIMES work, SOMETIMES not, depending on whether you get too much, too small, or just-right precision (which you cannot tell in advance).

Take this as a hint for the other postings being true.

Last week I saw a very intelligent rounding approach which was described in detail, it is based upon the same exponent **** as our comparator junk.

Some people call WR "WinRuiner" :-)

Regards.

Christoph Franzen
Durchwahl -907-2419
Schwarz, Prof. Spruth & Associates Unternehmensberatung AG & Co. KG
Deutsche Post World Net
ITSolution GmbH
Abt. SCN

4. ## Re: Rounding floats and sprintf()...

User (christopher.j.meisenzahl@citicorp.com.nospam) posted:

Christoph,

Thanks very much for replying! That's great info.

What exactly do you mean by "CompareFloat primitive"?

As an aside, could min() and max() be written as?
public function min ( in val1, in val2 )
{
return val1 < val2 ? val1 : val2;
}

public function max ( in val1, in val2 )
{
return val1 > val2 ? val1 : val2;
}

Thanks again,
Christopher
Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com
(585)-248-7749

5. ## Re: Rounding floats and sprintf()...

User (christopher.j.meisenzahl@citicorp.com.nospam) posted:

Christoph,

I forgot to ask this, the mailing list strip soff some brackets.

What lines are within the for loop within CompareFloat()?

Thanks,
Christopher
Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com
(585)-248-7749

6. ## Re: Rounding floats and sprintf()...

User Franzen, SPS PROFI, DA (C.Franzen@DeutschePost.de.nospam) posted:

The routine I posted. I call it a primitive since we base many other higher-level function on it (like IsGreater, and so on).

I think so :-)

You could also write

public function min(in val1,in val2) {return val1<val2?val1:val2;}public function max(in val1,in val2){return val1>val2?val1:val2;}

so save more space. (The WR parser is picky regarding blanks, so this might not work, but it´s a good joke :-)

There are no curly brackets there:

for (Exponent = 0; Exponent <= Decimals; Exponent++)
Multiplier *= 10 ;

So the only statement iterated is
Multiplier *= 10 ;

Regards.

Christoph Franzen
Durchwahl -907-2419
Schwarz, Prof. Spruth & Associates Unternehmensberatung AG & Co. KG
Deutsche Post World Net
ITSolution GmbH
Abt. SCN

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.