HyperCard Mailing List

[HC] How could it be true that (-1 < -1) ?
(Msg 1 of 6)
Arthur Evans Jr <evanssl21@[redacted].net>
Friday, 02-Nov-2012 18:40 GMT
If you use numberFormat "0.00", Hypercard displays a number like
'-0.12' as '-.12'. I think this is ugly and would prefer to display
it as '-0.12'.

So, I wrote a simple little function fixSmallNeg to fix small
negative numbers, and then (as I usually do) wrote a little test
program to see if I had gotten it right. I put the function and the
test on a button and ran it. Obviously I had problems or I wouldn't
be writing this note. The entire button script appears below.

Note the test in the third line of the function. On mathematical
grounds this test should never succeed, but in Hypercard's arithmetic
(actually the OS's floating point number arithmetic) the test does
succeed. When you enter the debugger you seem to find that for X = -1
it does fail. That is
-1 < -1
seems to be true. Really?

Now, replace the 'debug checkpoint' line by these 3 lines
set numberFormat to "0.00000000000000000"
debug checkpoint
set numberFormat to "0.00"
and you will find that
-1 < -.99999999999999952
which of course is true.

After messing around with this problem for a while trying to repair
my function I decided that I was stumped, and that it was time to
appeal to the wisdom of the list. Doing so seems to be particularly
timely given the recent discussion here.

Art Evans

--
[HC] How could it be true that (-1 < -1) ?
(Msg 2 of 6)
Michael Mays <michael@[redacted].org>
Friday, 02-Nov-2012 19:21 GMT
Arthur,

Just test for the exception.

if (char 1 of my_number is "-") then
if (char 2 of my_number is ".") then
put "-0" & character 2 to length(my_number) of my_number into my_number
my_number = "-0"&chars 1 to 1000 of my_number
end if
end if

Michael

On Nov 2, 2012, at 1:40 PM, Arthur Evans Jr <evanssl21@[redacted].net wrote:

> If you use numberFormat "0.00", Hypercard displays a number like
> '-0.12' as '-.12'. I think this is ugly and would prefer to display
> it as '-0.12'.
>
> So, I wrote a simple little function fixSmallNeg to fix small
> negative numbers, and then (as I usually do) wrote a little test
> program to see if I had gotten it right. I put the function and the
> test on a button and ran it. Obviously I had problems or I wouldn't
> be writing this note. The entire button script appears below.
>
> Note the test in the third line of the function. On mathematical
> grounds this test should never succeed, but in Hypercard's arithmetic
> (actually the OS's floating point number arithmetic) the test does
> succeed. When you enter the debugger you seem to find that for X = -1
> it does fail. That is
> -1 < -1
> seems to be true. Really?
>
> Now, replace the 'debug checkpoint' line by these 3 lines
> set numberFormat to "0.00000000000000000"
> debug checkpoint
> set numberFormat to "0.00"
> and you will find that
> -1 < -.99999999999999952
> which of course is true.
>
> After messing around with this problem for a while trying to repair
> my function I decided that I was stumped, and that it was time to
> appeal to the wisdom of the list. Doing so seems to be particularly
> timely given the recent discussion here.
>
> Art Evans
>
> --
[HC] How could it be true that (-1 < -1) ?
(Msg 3 of 6)
Michael Mays <michael@[redacted].org>
Friday, 02-Nov-2012 19:46 GMT
okay i didn't read your whole post or even the subject line . :(

0.1 has no exact base 2 representation. The result of this is that sometimes 1<> (1.3-0.1-0.1-0.1).
If you do the calculation in base 2 you will see what I mean.

Michael

On Nov 2, 2012, at 2:21 PM, Michael Mays <michael@[redacted].org wrote:

> Arthur,
>
> Just test for the exception.
>
> if (char 1 of my_number is "-") then
> if (char 2 of my_number is ".") then
> put "-0" & character 2 to length(my_number) of my_number into my_number
> my_number = "-0"&chars 1 to 1000 of my_number
> end if
> end if
>
> Michael
>
> On Nov 2, 2012, at 1:40 PM, Arthur Evans Jr <evanssl21@[redacted].net wrote:
>
>> If you use numberFormat "0.00", Hypercard displays a number like
>> '-0.12' as '-.12'. I think this is ugly and would prefer to display
>> it as '-0.12'.
>>
>> So, I wrote a simple little function fixSmallNeg to fix small
>> negative numbers, and then (as I usually do) wrote a little test
>> program to see if I had gotten it right. I put the function and the
>> test on a button and ran it. Obviously I had problems or I wouldn't
>> be writing this note. The entire button script appears below.
>>
>> Note the test in the third line of the function. On mathematical
>> grounds this test should never succeed, but in Hypercard's arithmetic
>> (actually the OS's floating point number arithmetic) the test does
>> succeed. When you enter the debugger you seem to find that for X = -1

>> it does fail. That is
>> -1 < -1
>> seems to be true. Really?
>>
>> Now, replace the 'debug checkpoint' line by these 3 lines
>> set numberFormat to "0.00000000000000000"
>> debug checkpoint
>> set numberFormat to "0.00"
>> and you will find that
>> -1 < -.99999999999999952
>> which of course is true.
>>
>> After messing around with this problem for a while trying to repair
>> my function I decided that I was stumped, and that it was time to
>> appeal to the wisdom of the list. Doing so seems to be particularly
>> timely given the recent discussion here.
>>
>> Art Evans
>>
>> --
[HC] How could it be true that (-1 < -1) ?
(Msg 4 of 6)
Arthur Evans Jr <evanssl21@[redacted].net>
Friday, 02-Nov-2012 20:43 GMT
Thanks.

The line
> my_number = "-0"&chars 1 to 1000 of my_number
isn't Hypertext and in any case isn't needed. Once I removed that it
worked. After some cleanup I have this

function fixSmallNeg X
if char 1 to 2 of X = "-." then
put "-0" & character 2 to length(X) of X into X
end if
return X
end fixSmallNeg

Now it works perfectly, and the HC list works again.

Art Evans


At 14:21 -0500 2012.11.02, Michael Mays wrote:
>Arthur,
>
>Just test for the exception.
>
> if (char 1 of my_number is "-") then
> if (char 2 of my_number is ".") then
> put "-0" & character 2 to length(my_number) of my_number into my_number
> my_number = "-0"&chars 1 to 1000 of my_number
> end if 
> end if
>
>Michael
>
>On Nov 2, 2012, at 1:40 PM, Arthur Evans Jr <evanssl21@[redacted].net wrote:
>
>> If you use numberFormat "0.00", Hypercard displays a number like
>> '-0.12' as '-.12'. I think this is ugly and would prefer to display
>> it as '-0.12'.
>>
>> So, I wrote a simple little function fixSmallNeg to fix small
>> negative numbers, and then (as I usually do) wrote a little test
>> program to see if I had gotten it right. I put the function and the
>> test on a button and ran it. Obviously I had problems or I wouldn't
>> be writing this note. The entire button script appears below.
>>
>> Note the test in the third line of the function. On mathematical
>> grounds this test should never succeed, but in Hypercard's arithmetic
>> (actually the OS's floating point number arithmetic) the test does
>> succeed. When you enter the debugger you seem to find that for X = -1
>> it does fail. That is
>> -1 < -1
>> seems to be true. Really?
>>
>> Now, replace the 'debug checkpoint' line by these 3 lines
>> set numberFormat to "0.00000000000000000"
>> debug checkpoint
>> set numberFormat to "0.00"
>> and you will find that
>> -1 < -.99999999999999952
>> which of course is true.
>>
>> After messing around with this problem for a while trying to repair
>> my function I decided that I was stumped, and that it was time to
>> appeal to the wisdom of the list. Doing so seems to be particularly
>> timely given the recent discussion here.
>>
>> Art Evans
>>
>> --
[HC] How could it be true that (-1 < -1) ?
(Msg 5 of 6)
Michael Mays <michael@[redacted].org>
Friday, 02-Nov-2012 21:01 GMT
You must not be running version 3. :)
Michael

On Nov 2, 2012, at 3:43 PM, Arthur Evans Jr <evanssl21@[redacted].net wrote:

> Thanks.
>
> The line
>> my_number = "-0"&chars 1 to 1000 of my_number
> isn't Hypertext and in any case isn't needed. Once I removed that it
> worked. After some cleanup I have this
>
> function fixSmallNeg X
> if char 1 to 2 of X = "-." then
> put "-0" & character 2 to length(X) of X into X
> end if
> return X
> end fixSmallNeg
>
> Now it works perfectly, and the HC list works again.
>
> Art Evans
>
>
> At 14:21 -0500 2012.11.02, Michael Mays wrote:
>> Arthur,
>>
>> Just test for the exception.
>>
>> if (char 1 of my_number is "-") then
>> if (char 2 of my_number is ".") then
>> put "-0" & character 2 to length(my_number) of my_number into my_number
>> my_number = "-0"&chars 1 to 1000 of my_number
>> end if
>> end if
>>
>> Michael
>>
>> On Nov 2, 2012, at 1:40 PM, Arthur Evans Jr <evanssl21@[redacted].net wrote:
>>
>>> If you use numberFormat "0.00", Hypercard displays a number like
>>> '-0.12' as '-.12'. I think this is ugly and would prefer to display
>>> it as '-0.12'.
>>>
>>> So, I wrote a simple little function fixSmallNeg to fix small
>>> negative numbers, and then (as I usually do) wrote a little test
>>> program to see if I had gotten it right. I put the function and the
>>> test on a button and ran it. Obviously I had problems or I wouldn't
>>> be writing this note. The entire button script appears below.
>>>
>>> Note the test in the third line of the function. On mathematical
>>> grounds this test should never succeed, but in Hypercard's arithmetic
>>> (actually the OS's floating point number arithmetic) the test does
>>> succeed. When you enter the debugger you seem to find that for X = -1
>>> it does fail. That is
>>> -1 < -1
>>> seems to be true. Really?
>>>
>>> Now, replace the 'debug checkpoint' line by these 3 lines
>>> set numberFormat to "0.00000000000000000"
>>> debug checkpoint
>>> set numberFormat to "0.00"
>>> and you will find that
>>> -1 < -.99999999999999952
>>> which of course is true.
>>>
>>> After messing around with this problem for a while trying to repair
>>> my function I decided that I was stumped, and that it was time to
>>> appeal to the wisdom of the list. Doing so seems to be particularly
>>> timely given the recent discussion here.
>>>
>>> Art Evans
>>>
>>> --
[HC] How could it be true that (-1 < -1) ?
(Msg 6 of 6)
DunbarX@[redacted].com <DunbarX@[redacted].com>
Friday, 02-Nov-2012 23:15 GMT
Arthur.


What Michael meant is that what you see is not exactly what the machine gets. When he says there is no exact representation of "0.1" in base 2, where all computers do their calculating, he meant that what computers actually do is the best they can, and there is generally an inexact representation somewhere in the 19th decimal.


We rarely have to deal with that level of precision, so it usually goes unnoticed. But there are simpler ways to seemingly find arithmetic or comparative errors if this is not understood.


And accepted with forbearance.


Craig Newman


-----Original Message-----
From: Arthur Evans Jr <evanssl21@[redacted].net
To: HyperCard <HyperCard-Mailing-List>
Cc: HyperCard <HyperCard-Mailing-List>
Sent: Fri, Nov 2, 2012 4:47 pm
Subject: Re: [HC] How could it be true that (-1 < -1) ?





Thanks.

The line
> my_number = "-0"&chars 1 to 1000 of my_number
isn't Hypertext and in any case isn't needed. Once I removed that it
worked. After some cleanup I have this

function fixSmallNeg X
if char 1 to 2 of X = "-." then
put "-0" & character 2 to length(X) of X into X
end if
return X
end fixSmallNeg

Now it works perfectly, and the HC list works again.

Art Evans

At 14:21 -0500 2012.11.02, Michael Mays wrote:
>Arthur,
>
>Just test for the exception.
>
> if (char 1 of my_number is "-") then
> if (char 2 of my_number is ".") then
> put "-0" & character 2 to length(my_number) of my_number into my_number
> my_number = "-0"&chars 1 to 1000 of my_number
> end if
> end if
>
>Michael
>
>On Nov 2, 2012, at 1:40 PM, Arthur Evans Jr <evanssl21@[redacted].net wrote:
>
>> If you use numberFormat "0.00", Hypercard displays a number like
>> '-0.12' as '-.12'. I think this is ugly and would prefer to display
>> it as '-0.12'.
>>
>> So, I wrote a simple little function fixSmallNeg to fix small
>> negative numbers, and then (as I usually do) wrote a little test
>> program to see if I had gotten it right. I put the function and the
>> test on a button and ran it. Obviously I had problems or I wouldn't
>> be writing this note. The entire button script appears below.
>>
>> Note the test in the third line of the function. On mathematical
>> grounds this test should never succeed, but in Hypercard's arithmetic
>> (actually the OS's floating point number arithmetic) the test does
>> succeed. When you enter the debugger you seem to find that for X = -1
>> it does fail. That is
>> -1 < -1
>> seems to be true. Really?
>>
>> Now, replace the 'debug checkpoint' line by these 3 lines
>> set numberFormat to "0.00000000000000000"
>> debug checkpoint
>> set numberFormat to "0.00"
>> and you will find that
>> -1 < -.99999999999999952
>> which of course is true.
>>
>> After messing around with this problem for a while trying to repair
>> my function I decided that I was stumped, and that it was time to
>> appeal to the wisdom of the list. Doing so seems to be particularly
>> timely given the recent discussion here.
>>
>> Art Evans
>>
>> --
HyperCard® and HyperTalk™ remain trademarks of Apple, Inc.; other trademarked products and terms mentioned in this archive are the property of their respective trademark holders. Individual messages remain the intellectual property of their respective authors.