473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding multiple of a decimal number in a floating point list

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.
Aug 18 '08 #1
2 5203
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
Aug 18 '08 #2
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
Aug 18 '08 #3

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

Similar topics

21
4518
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
17
6135
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
7
43161
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 have an array of 50 doubles that each ahs a number such as 23.9918444. I want to round round this number to 23.99 and any other calculations done on it should have the same precision. I know that Decimal Format does the rounding but the thing that...
687
23321
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 believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
15
12811
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 last):
18
12160
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 number of digits in a given number. I request to help me out in solving the below said question. Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
11
2238
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 Single with value 4.475 was converted to a Decimal with value 4.4749999999999996D. So after inserting and selecting it from the database I got another value than the original!
19
3594
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 starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
23
2236
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 moment I use this terrible substitute: FLOAT_PREC = repr(1.0/3).count('3')
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.