Thanks:  0
Likes:  0
Dislikes:  0

1. sprintf problem

User Shum, Garry (garry.shum@rbc.com.nospam) posted:

------_=_NextPart_001_01C21797.34890250
Content-Type: text/plain;
charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

I use sprintf() to round off numbers. However, I've found that if the
number located at the decimal place I want to round off at is a 5, it will
incorrectly round off to the lower rather than the next higher decimal
place. For example...

number =3D 2.925;

...should equal 2.93, but instead it equals 2.92 which is incorrect. How d=
o
I solve this?

ps. I already wrote another number rounding function but I'd rather not use
it because it runs more slowly than using sprintf() especially when I need
to embed my number rounding function within a loop structure.
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
----------------------------------------------------------------
This e-mail may be privileged and/or confidential, and the sender does not =
waive any related rights and obligations. Any distribution, use or copying =
of this e-mail or the information it contains by other than an intended rec=
me (by return e-mail or otherwise) immediately.=20

Ce courriel est confidentiel et prot=E9g=E9. L'exp=E9diteur ne renonce pas =
aux droits et obligations qui s'y rapportent. Toute diffusion, utilisation =
ou copie de ce message ou des renseignements qu'il contient par une personn=
e autre que le (les) destinataire(s) d=E9sign=E9(s) est interdite. Si vous =
recevez ce courriel par erreur, veuillez m'en aviser imm=E9diatement, par r=
etour de courriel ou par un autre moyen.=20

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D

------_=_NextPart_001_01C21797.34890250
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; charset=3Diso-8859-=
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version 5.5.2654.19"=
found that if the number located at the decimal place I want to round off =
at is a 5, it will incorrectly round off to the lower rather than the next =
higher decimal place. For example...</FONT></P>

incorrect. How do I solve this?</FONT>

I'd rather not use it because it runs more slowly than using sprintf() espe=
cially when I need to embed my number rounding function within a loop struc=

Post message: winrunner@yahoogroups.com

Subscribe: winrunner-subscribe@yahoogroups.com

Unsubscribe: winrunner-unsubscribe@yahoogroups.com

***To unsubscribe via email: From your email program, send a blank message =
to winrunner-unsubscribe@yahoogroups.com.

*To respond offline to an individual, locate the sender's email address in =
the email message

*To view archives go to groups.yahoo.com

Eric Marx
---------------------------------------------------------------------------=
This e-mail may be privileged and/or confidential, and the sender does not =
waive any related rights and obligations. Any distribution, use or copying =
of this e-mail or the information it contains by other than an intended rec=
me (by return e-mail or otherwise) immediately.

Ce courriel est confidentiel et prot=E9g=E9. L'exp=E9diteur ne renonce pas =
aux droits et obligations qui s'y rapportent. Toute diffusion, utilisation =
ou copie de ce message ou des renseignements qu'il contient par une personn=
e autre que le (les) destinataire(s) d=E9sign=E9(s) est interdite. Si vous =
recevez ce courriel par erreur, veuillez m'en aviser imm=E9diatement, par r=
etour de courriel ou par un autre moyen.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=

------_=_NextPart_001_01C21797.34890250--

2. Re: sprintf problem

I think function sprintf rounds to the inmediate upper digit if the number is .51 or greater, because:

number = sprintf("%10.2f",2.9251); is equal to 2.93 but,

number = sprintf("%10.2f",2.9250); is equal to 2.92.

------------------

3. Re: sprintf problem

I suspect this is due to the innate imprecision of floating point arithmetic. Try the following:

public test[] = {2.105, 2.115, 2.125, 2.135, 2.145, 2.155, 2.165, 2.175, 2.185, 2.195};
for(ix = 0; ix in test; ix++)
{
printf("%f rounds to %.2f", test[ix], test[ix]);
}

For me it returned the following:

2.105000 rounds to 2.11
2.115000 rounds to 2.12
2.125000 rounds to 2.13
2.135000 rounds to 2.13
2.145000 rounds to 2.15
2.155000 rounds to 2.15
2.165000 rounds to 2.17
2.175000 rounds to 2.17
2.185000 rounds to 2.19
2.195000 rounds to 2.19

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

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

4. Re: sprintf problem

User Misha (mm_ke@yahoo.com.nospam) posted:

Hi, I did a bit of playing, perhaps a bit over the top but it was fun

Note the sprintf rounding is not consistent:
sprintf("%.2f",2.295) gives 2.29
sprintf("%.2f",2.395) gives 2.40

bring it to a single decimal place, it gets more accurate:
sprintf("%.0f",229.5) gives 230

so to bring it to the single decimal place, multiply by powers of ten:

number*10**decimalplaces

Now if we round to first decimal place, using either of:
int(n+0.5)
sprintf("%.0f",n)

Theoretically it should work ok, but some numbers dont because they
still are represented internally with too much unwanted precision,
so can stringify them before using them... add to an empty string

int(""number*10**decimalplaces+0.5) gives the right result

This works much better, hooray for dirty WR workarounds.

So now we divide by all those powers of ten and represent in desired form:

sprintf("%." decimalplaces "f", int(""
number*10**decimalplaces+0.5)/10**decimalplaces)
sprintf("%." decimalplaces "f", sprintf("%.0f",""
number*10**decimalplaces)/10**decimalplaces)

Both the above should give the same result. (zero decimal places works also)

I don't know how this will compare speedwise!

Kind regards,
Misha.
--------------------------------
From: "Shum, Garry" <garry.shum@r...>
Date: Wed Jun 19, 2002 10:38 pm
Subject: RE: [winrunner] sprintf problem

Yes, but the 0.005 does not adjust for changes to how many decimal places is
being used. If you use 5 decimal places, you know you would add 0.000005,
but the number "0.000005" has to be dynamically created, not hard-coded into
your code. that's why I used what I put in my last message. It does
exactly what you do here, except it dynamically changes the "0.000005" to
account for whatever number of decimal places is being used...
public function number_round(in number, in decimalplaces, out answer) {
auto percentpos; #byte position of percentage sign if it exists within
given number
sprintf() problem

#Make sure 2nd parameter is legal
if (isNumber(decimalplaces,"","","") == FALSE)
return E_ILLEGAL_PARAMETER;

#Check if given number is a percentage
if (match(number,"%") != 0)
percentpos = match(number,"%");

#Round given value
if (decimalplaces > 0) {
if (substr(number,match(number,"\.")+decimalplaces+1, 1) == 5)
}

if (percentpos != "") {
if (percentpos == 1)
else
}

return E_OK;
}

5. Re: sprintf problem

User Shum, Garry (garry.shum@rbc.com.nospam) posted:

------_=_NextPart_001_01C21865.B3BA74E0
Content-Type: text/plain;
charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

I clearly remember a problem with Winrunner number handling where numerical
calculations would result in a slight difference between what was actually
stored in memory and what WinRunner displayed in the watch list. So, you
could end up with situations where...
=20
a =3D 5;
if (a =3D=3D 5)
a +=3D 5;
=20
...a would equal 5 rather than 10 in the above situation. This was because
the storage of "a" could actually be something like... 5.000000001 rather
than simply 5. This is just an example and does work, but there are some
situations where something like this would break because of the imprecision
of what is actually be stored in memory vs. what is seen from the watch
list.
=20
=20
=20
=20
=20
=20

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.