[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why do some math functions return -0
- From: HyperHacker <hyperhacker@...>
- Date: Sun, 19 Sep 2010 14:45:36 -0600
On Sun, Sep 19, 2010 at 13:49, David Kastrup <dak@gnu.org> wrote:
> caseyh@istar.ca writes:
>
>> I haven't read all the replies; in some languages the modulus function
>> is implemented differently for negative numbers. :)
>
> Where do you see a negative number?
>
>> Quoting Mike McGonagle <mjmogo@gmail.com>:
>>
>>> Well, here is the code that I am using, simplified to remove all the
>>> tables...
>>>
>>> order = 3
>>> elements = order * order
>>> size = elements * elements
>>>
>>> for i = 0, (size - 1) do
>>> r = math.floor(i / elements)
>>> c = math.floor(i % elements)
>>> b = math.floor(math.floor(math.floor(r / order) * order) +
>>> math.floor(c / order))
>>> br = math.floor(r % order)
>>> bc = math.floor(c % order)
>>> print(string.format("i:%f r: %f c: %f b:%f br:%f bc:%f", i, r,
>>> c, b, br, bc))
>>> end
>>>
>>> I would think that because all of the numbers that I am working with,
>>> all the numbers would come out positive, but many of the resulting
>>> '0's come out as -0. While I have tried to preliminary test to see if
>>> "0 == -0", I am just wondering if by chance this is due to some
>>> rounding error.
>>>
>>> This is something that has bothered me about Lua for a while now, as I
>>> don't really understand what '-0' really is or means. I mean,
>>> logically, I can understand that there is probably some use for it,
>>> but for the things that I am doing, I see absolutely no reason for it,
>>> as I am trying to work with nothing but positive integers.
>>>
>>> Thanks,
>>>
>>> Mike
>>>
>>> On Sun, Sep 19, 2010 at 11:53 AM, Drake Wilson <drake@begriffli.ch> wrote:
>>>> Quoth Mike McGonagle <mjmogo@gmail.com>, on 2010-09-19 11:19:59 -0400:
>>>>> Hello all,
>>>>>
>>>>> I have been doing some very simple things (mostly to get the integer
>>>>> portion of a number) using math.floor, and it seems that sometimes the
>>>>> function will return -0, when I am expecting a 0. Can someone please
>>>>> explain why this happens? Is there a logical reason for this?
>>>>
>>>> Why wouldn't they? Negative and positive zero are two floating-point
>>>> representations with the same closest real number.
>>>>
>>>> Can you give a specific example?
>>>>
>>>>> Thanks,
>>>>>
>>>>> Mike
>>>>
>>>> ---> Drake Wilson
>>>>
>>>>
>>>
>>>
>>
>>
>>
>>
>
> --
> David Kastrup
>
>
>
Negative zero is an artifact of how some systems implement some
floating-point operations. Unlike integers, floating-point values have
a sign bit. Some systems won't clear the sign when the result is zero,
hence -0. It should function identically to zero in all operations,
including comparing to zero. I can't confirm that it does, as I can't
seem to generate one.
Note that a number displayed as -0.0000 could also be a very small
negative number rounded to fit the display, e.g.:
> =('%f'):format(-0.00000001)
-0.000000
%f rounds to six places by default. This is the proper meaning of
negative zero - a negative value that rounded to zero.
It'd probably save you a lot of headache in the future to read up on
how floating-point works, as it has a lot of potential pitfalls.
--
Sent from my toaster.