473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Rounding up to the nearest exact logarithmic decade

Given a value (x) that is within the range (1e-1, 1e7) how do I round
(x) up to the closest exact logarithmic decade? For instance:

10**3 = 1000
x = 4978
10**4 = 10000

x = 10000

Thanks Everyone!
Derek Basch

Feb 28 '06 #1
8 6000
Derek Basch wrote:
Given a value (x) that is within the range (1e-1, 1e7) how do I round
(x) up to the closest exact logarithmic decade? For instance:

10**3 = 1000
x = 4978
10**4 = 10000
x = 10000


how about
import math
def roundup(x): ... return 10**math.ceil(math.log10(x))
... roundup(4978)

10000.0

?

</F>

Feb 28 '06 #2
jao
Quoting Derek Basch <db****@yahoo.com>:
Given a value (x) that is within the range (1e-1, 1e7) how do I round
(x) up to the closest exact logarithmic decade? For instance:


How about this:

def roundup(x):
if x < 1:
return 1
else:
return '1' + ('0' * len(str(int(x))))

Jack Orenstein
Feb 28 '06 #3
Em Ter, 2006-02-28 Ã*s 17:47 -0500, ja*@geophile.com escreveu:
Quoting Derek Basch <db****@yahoo.com>:
Given a value (x) that is within the range (1e-1, 1e7) how do I round
(x) up to the closest exact logarithmic decade? For instance:


How about this:

def roundup(x):
if x < 1:
return 1
else:
return '1' + ('0' * len(str(int(x))))


No dice. First, it returns an int for some cases and a string for the
others. Second, casting from str to int and vice-versa and concatenating
strings won't perform any good. I wouldn't like this hack on my code.

My 2¢,
Felipe.

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Feb 28 '06 #4
Thanks effbot. I knew their had to be something buried in the math
module that could help. ceil() it is!

</dTb>

Feb 28 '06 #5
On 2006-02-28, ja*@geophile.com <ja*@geophile.com> wrote:
Quoting Derek Basch <db****@yahoo.com>:
Given a value (x) that is within the range (1e-1, 1e7) how do I round
(x) up to the closest exact logarithmic decade? For instance:


How about this:

def roundup(x):
if x < 1:
return 1
else:
return '1' + ('0' * len(str(int(x))))


Interesting approach. You're converting to string at the same
time. I would guess the OP wanted a float.

--
Grant Edwards grante Yow! It's OKAY -- I'm an
at INTELLECTUAL, too.
visi.com
Feb 28 '06 #6
I like Fredrik's solution. If for some reason you are afraid of
logarithms, you could also do:
x = 4978
decades = [10 ** n for n in xrange(-1,8)]
import itertools
itertools.ifilter(lambda decade: x < decade, decades).next()

10000

BTW, are the python-dev guys aware that 10 ** -1 = 0.10000000000000001 ?

Feb 28 '06 #7
On 2006-02-28, jo********@gmail.com <jo********@gmail.com> wrote:
I like Fredrik's solution. If for some reason you are afraid of
logarithms, you could also do:
x = 4978
decades = [10 ** n for n in xrange(-1,8)]
import itertools
itertools.ifilter(lambda decade: x < decade, decades).next()

10000

BTW, are the python-dev guys aware that 10 ** -1 = 0.10000000000000001 ?


You're joking, right?

--
Grant Edwards grante Yow! HOORAY, Ronald!! Now
at YOU can marry LINDA
visi.com RONSTADT too!!
Feb 28 '06 #8
jo********@gmail.com wrote:
BTW, are the python-dev guys aware that 10 ** -1 = 0.10000000000000001 ?


http://docs.python.org/tut/node16.html

</F>

Mar 1 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: b | last post by:
Hello all, we have a table that is part of our accounting package that stores decimals as such: 123.45678. However the reporting standards with the accounting system display that as 123.45 note...
8
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are...
6
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp ---------------------...
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
5
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.