Re: Rob's going to hate me... (Remainder bug)

new topic     » goto parent     » topic index » view thread      » older message » newer message

----- Original Message ----- 
From: <kbochert at copper.net>
To: <EUforum at topica.com>
Subject: Re: Rob's going to hate me... (Remainder bug)


> 
> 
> On 8 Nov 2003 at 8:10, Derek Parnell wrote:
> 
> > 
> > ----- Original Message ----- 
> > From: "Urzumph" <Urzumph at HotPOP.com>
> > To: "Mailing List" <EUforum at topica.com>
> > Sent: Friday, November 07, 2003 11:19 AM
> > Subject: Rob's going to hate me... (Remainder bug)
> > 
> > 
> > > Heh, twice in a week, lol
> > > ? remainder(100,0.01)
> > > gives 0.01 and I am sure that's not supposed to happen
> > > This is reproducable with all numbers <= 0.2
> > > for some reason, all numbers > .2 work as they should.
> > > Also, this only affects numbers which are cleanly divisable.
> > > ? remainder(100.001,0.01) returns 0.001 as it should
> > > 
> > > Running Euphoria 2.4
> > > 
> > 
> > Try this then...
> > 
> > function rmder(atom a, atom b)
> > atom x
> >     x = a / b
> >     return (x - floor(x)) * b
> > end function
> > 
> > 
> > ? remainder(100, 0.01)
> > ? rmder(100, 0.01)
> > 
> > ? remainder(100, 0.2)
> > ? rmder(100, 0.2)
> > 
> > ? remainder(100, 1.01)
> > ? rmder(100, 1.01)
> > 
> Yes, but don't forget about negative arguments.
> remainder, like fmod(), is defined to return a result of the same sign 
> as its first argument.
> (took me about a dozen tries to get it right!)
> ktb
> 

Ok then, how about ...

function rmder(atom a, atom b)
    atom x  
    integer sign 

    if a < 0 then
        sign = -1
        a = -a
    else
        sign = 1
    end if
    if b < 0 then
        b = -b
    end if
    x = a / b
    return ((x - floor(x)) * b) * sign
end function

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu