Hi:
I need to find the multiples of a decimal number in a floating point
list. For instance, if a have the list [0,0.01,0.02,...1], I want the
multiples of 0.2: [0, 0.2,0.4,0.6,0.8,1].
With integers this problem is easy, just test for (i%n == 0), where i
is the number I am testing, and n is the multiple. Given the finite
resolution of floating point numbers, this is more complicated for
float.
I came with this solution:
from numpy import arange
from math import modf, fabs
float_range = arange(0, 1, 0.01)
for f in float_range:
m = modf(f / 0.2)[0]
if m<1e-13 or fabs(1-m)<1e-13:
print f
# Do something else
This code works, however, I found it a little ugly. Is there a better
way to do the same?
Alejandro. 2 5182
On Mon, 18 Aug 2008 10:52:45 -0700, Alejandro wrote:
Hi:
I need to find the multiples of a decimal number in a floating point
list. For instance, if a have the list [0,0.01,0.02,...1], I want the
multiples of 0.2: [0, 0.2,0.4,0.6,0.8,1].
With integers this problem is easy, just test for (i%n == 0), where i is
the number I am testing, and n is the multiple. Given the finite
resolution of floating point numbers, this is more complicated for
float.
I came with this solution:
from numpy import arange
from math import modf, fabs
float_range = arange(0, 1, 0.01)
for f in float_range:
m = modf(f / 0.2)[0]
if m<1e-13 or fabs(1-m)<1e-13:
print f
# Do something else
This code works, however, I found it a little ugly. Is there a better
way to do the same?
Alejandro.
Hi Alejandro, you can do the same thing more efficiently (both in terms
of lines of code and execution speed) by doing the whole array at once:
from numpy import arange, absolute
from math import modf, fabs
float_range = arange(0, 1, 0.01)
multiples = absolute(float_range % 0.2)<1e-13
# now multiples is a boolean array
print float_range[multiples]
HTH,
Dan
On Mon, 18 Aug 2008 18:04:53 +0000, Dan Lenski wrote:
On Mon, 18 Aug 2008 10:52:45 -0700, Alejandro wrote:
>Hi:
I need to find the multiples of a decimal number in a floating point list. For instance, if a have the list [0,0.01,0.02,...1], I want the multiples of 0.2: [0, 0.2,0.4,0.6,0.8,1].
With integers this problem is easy, just test for (i%n == 0), where i is the number I am testing, and n is the multiple. Given the finite resolution of floating point numbers, this is more complicated for float.
I came with this solution:
from numpy import arange from math import modf, fabs
float_range = arange(0, 1, 0.01) for f in float_range: m = modf(f / 0.2)[0] if m<1e-13 or fabs(1-m)<1e-13: print f # Do something else
This code works, however, I found it a little ugly. Is there a better way to do the same?
Alejandro.
Hi Alejandro, you can do the same thing more efficiently (both in terms
of lines of code and execution speed) by doing the whole array at once:
from numpy import arange, absolute
from math import modf, fabs
float_range = arange(0, 1, 0.01)
multiples = absolute(float_range % 0.2)<1e-13 # now multiples is a
boolean array
print float_range[multiples]
HTH,
Dan
Oh, I forgot that you have to check the other case of it being slightly
less than a multiple too!
from numpy import arange, absolute
from math import modf, fabs
float_range = arange(0, 1, 0.01)
mod = absolute(float_range % 0.2)
multiples = (mod < 1e-13) + (mod 0.2-1e-13)
print float_range[multiples]
That should do the trick!
Dan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Batista, Facundo |
last post by:
Here I send it.
Suggestions and all kinds of recomendations are more than welcomed.
If it all goes ok, it'll be a PEP when I finish writing/modifying the code.
Thank you.
.. Facundo
|
by: John Bentley |
last post by:
John Bentley:
INTRO
The phrase "decimal number" within a programming context is ambiguous. It could
refer to the decimal datatype or the related but separate concept of a generic
decimal number....
|
by: hana1 |
last post by:
Hello experts,
I used to program in C/C++ and now switched to Java. I am having a
difficulty that I need your help with. How can I limit a double variable to
hold 2 decimal points only? Say I...
|
by: cody |
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't...
|
by: Kay Schluehr |
last post by:
I wonder why this expression works:
>>> decimal.Decimal("5.5")**1024
Decimal("1.353299876254915295189966576E+758")
but this one causes an error
5.5**1024
Traceback (most recent call...
| |
by: Kuljit |
last post by:
I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the...
|
by: Pieter |
last post by:
Hi,
I'm having some troubles with my numeric-types in my VB.NET 2005
application, together with a SQL Server 2000.
- I first used Single in my application, and Decimal in my database. But a...
|
by: VK |
last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/
b495b4898808fde0>
is more than one month old - this may pose problem for posting over
some news servers. This is why I'm...
|
by: Arnaud Delobelle |
last post by:
Hi all,
I want to know the precision (number of significant digits) of a float
in a platform-independent manner. I have scoured through the docs but
I can't find anything about it!
At the...
|
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,...
|
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...
| |
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...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |