Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Gold

Gold

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #15 on Mon 03 Jul 2006 05:12 PM (UTC)
Message
%s is for strings, %d is for numbers. num_punct must be a function that converts doubles to strings. What is it that you want to do here? I guess I don't understand your question. :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Metsuro   USA  (389 posts)  Bio
Date Reply #16 on Mon 03 Jul 2006 05:14 PM (UTC)
Message
oh... wow I didn't even notice that...

Everything turns around in the end
Top

Posted by Nick Gammon   Australia  (23,163 posts)  Bio   Forum Administrator
Date Reply #17 on Tue 04 Jul 2006 11:55 PM (UTC)
Message
Quote:

But for your case, I suspect it might be constant propagation ...


I can believe that for the case:


print (((1 / 3) * 3) == 1) --> true


However if I do these as separate commands in the command window, Lua can hardly know to optimize away a divide when it hasn't seen the multiply yet:


/a = 1/3
/print (a) --> 0.33333333333333
/print (a * 3) --> 1
/print (a * 3 == 1) --> true



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #18 on Wed 05 Jul 2006 04:30 AM (UTC)
Message
Yeah, you're right, it is pretty surprising. I did some further experiments of my own, trying to confuse it, and in order for it to do constant folding it would have to go back across the previous assignments of all variables which I truly doubt Lua does.

Perhaps doubles have some way of noting 'infinity', so that 1/3 is represented as 0.3bar (where 'bar' means 'repeat forever'). Mathematically 0.9bar is defined as 1 (I don't like that definition, but it's the "rule"), so 0.3bar times three would be 0.9bar which would be one.

If I had the time I'd look into how all this actually works. Maybe I will, in fact, sometime soon... this is quite interesting. :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,163 posts)  Bio   Forum Administrator
Date Reply #19 on Wed 05 Jul 2006 09:19 AM (UTC)
Message
I think you are making an assumption here, and that assumption is that numbers are stored internally to the base 10.

Hypothetically, if doubles were stored to the base 3, then 1/3 could be represented exactly.

More realistically, the scenario of 1/3 * 3, might result in a number that is so close to 1, that the hardware rounds it up to 1.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #20 on Wed 05 Jul 2006 01:41 PM (UTC)
Message
Well, numbers are stored to the base 2 sort of a fortiori, no? If I remember correctly, floating point numbers have two components to them: the digits and the exponent, and those are both basically binary numbers and the hardware knows how to convert them to the proper form.

Besides, this also works for 1/7 * 7, and the base 3 assumption wouldn't really help there.

It seems to me that rounding is more likely what's going on here. I'm sure you could find something such that playing around with it would in fact break it.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,163 posts)  Bio   Forum Administrator
Date Reply #21 on Wed 05 Jul 2006 09:37 PM (UTC)
Message
Quote:

Well, numbers are stored to the base 2 sort of a fortiori, no?


Well, probably they are today, yes. However I am pointing out the assumption there. Years ago I used to work on Burroughs Medium Systems (B 4700 etc.) and they stored memory addressable to 4 bits, not 8 bits as is commonly done these days. The 4-bit unit was called a "nibble" (half a byte), and I think also a "digit". Since 4 bits could represent 0-9, plus A-F, numbers were stored decimally. Thus the number 12345 would be stored in memory as (in hex): 12345.

Quote:

I'm sure you could find something such that playing around with it would in fact break it.


A script seems called for:



for i = 1, 1000 do
  j = 1 / i
  k = j * i
  assert (k == 1, "Failed at " .. i)
end -- for


When run I get: Failed at 49

This confirms it:


print (string.format ("%2.20f", ((1 / 49) * 49))) --> 0.99999999999999989000


If you juggle around the script and work with a starting number other than 1 you can make it fail earlier or later:


base = 1000

for i = 1, 100000 do
  j = base / i
  k = j * i
  assert (k == base, "Failed at " .. i)
end -- for


This shows: Failed at 15

I think the point of the Lua developers was that doubles will work fine for whole numbers up to a fairly large number.

If you are in fact going to be doing division on your currency, then long long will probably be worse anyway, as it simply will throw away information much faster than a double will.

For example:


long long i = 1;
long long j = i / 2;
long long k = j * 2; // k is now zero, not 1




- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


60,663 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.