div() for Floatingpoint number

212 views
Skip to first unread message

Sisyphuss

unread,
Oct 8, 2015, 11:01:07 AM10/8/15
to julia-users
Hello everyone,

I want a function F(), so that
F(0.154, 0.1)   = 0.1
F(0.154, 0.01) = 0.15
F(0.154, 0.003) = 0.153

I tried `div(a,b)*b`. However, I got:
div(0.128,0.0001) * 0.0001 = 0.1279
not equal to 0.1280

Any solution?

Isaiah Norton

unread,
Oct 8, 2015, 11:50:40 AM10/8/15
to julia...@googlegroups.com

Stefan Karpinski

unread,
Oct 9, 2015, 6:24:51 AM10/9/15
to Julia Users
What is the behavior of this function supposed to be? F(a,b) is the nearest multiple of b to a? I'm curious what definition is consistent with all three given examples.

Sisyphuss

unread,
Oct 9, 2015, 10:10:24 AM10/9/15
to julia-users
Yes, precisely downwards nearest multiple.
The original idea is whether a function can return a value to a given precision (something like floor, round).
Furthermore, it will be more useful if it works for more general precision, say 0.05, 0.02, 0.03...

Dan

unread,
Oct 9, 2015, 12:39:15 PM10/9/15
to julia-users
F(a,b) = floor(Int,a/b)*b

seems to fit the examples.

Stefan Karpinski

unread,
Oct 10, 2015, 1:18:32 AM10/10/15
to Julia Users
On Fri, Oct 9, 2015 at 9:44 PM, Dan <get...@gmail.com> wrote:
F(a,b) = floor(Int,a/b)*b

Ok, that's a good answer then.

Sisyphuss

unread,
Oct 12, 2015, 8:48:19 AM10/12/15
to julia-users
Thanks, that seems to be a good idea!
Reply all
Reply to author
Forward
0 new messages