Prev: lisp port
Next: Disjoint sets
From: Pascal J. Bourguignon on 24 May 2010 18:36 G�nther Thomsen <guenthert(a)gmail.com> writes: > On May 23, 6:22�am, p...(a)informatimago.com (Pascal J. Bourguignon) > wrote: >> and on the hand, why would you write: >> >> � � 2.5 >> �* �4.00 >> ----------------- >> � �10.000 >> >> and not just 10. >> >> Notice that the expectation here is that: >> � � � 2.5 * 4.00 �= 10.000 >> but: >> � � � 2.5 * 4.000 = 10.0000 >> which is quite a different result. >> > Would you mind explaining where the additional precision comes from? It comes from arithmetics. Try it with: 2.3 * 5.12 = 11.776 2.5 * 4.123 = 11.7875 >> If you are working with physical quantities, the expectation is to >> preserve the precision. � > If you want to be correct, you use error propagation. For high school > math you might get away with "significance arithmetic" > http://en.wikipedia.org/wiki/Significance_arithmetic -- __Pascal Bourguignon__ http://www.informatimago.com/
From: Günther Thomsen on 24 May 2010 22:02 On May 24, 3:36 pm, p...(a)informatimago.com (Pascal J. Bourguignon) wrote: > Günther Thomsen <guenth...(a)gmail.com> writes: > > On May 23, 6:22 am, p...(a)informatimago.com (Pascal J. Bourguignon) > > wrote: > >> and on the hand, why would you write: > > >> 2.5 > >> * 4.00 > >> ----------------- > >> 10.000 > > >> and not just 10. > > >> Notice that the expectation here is that: > >> 2.5 * 4.00 = 10.000 > >> but: > >> 2.5 * 4.000 = 10.0000 > >> which is quite a different result. > > > Would you mind explaining where the additional precision comes from? > > It comes from arithmetics. > > Try it with: > > 2.3 * 5.12 = 11.776 > 2.5 * 4.123 = 11.7875 > Well here you imply that all numbers are exact. Above you implied a precision given by the number of digits presented (see "significance arithmetic" linked to above). What are we talking about?
From: Pascal J. Bourguignon on 25 May 2010 03:08 G�nther Thomsen <guenthert(a)gmail.com> writes: > On May 24, 3:36�pm, p...(a)informatimago.com (Pascal J. Bourguignon) > wrote: >> G�nther Thomsen <guenth...(a)gmail.com> writes: >> > On May 23, 6:22�am, p...(a)informatimago.com (Pascal J. Bourguignon) >> > wrote: >> >> and on the hand, why would you write: >> >> >> � � 2.5 >> >> �* �4.00 >> >> ----------------- >> >> � �10.000 >> >> >> and not just 10. >> >> >> Notice that the expectation here is that: >> >> � � � 2.5 * 4.00 �= 10.000 >> >> but: >> >> � � � 2.5 * 4.000 = 10.0000 >> >> which is quite a different result. >> >> > Would you mind explaining where the additional precision comes from? >> >> It comes from arithmetics. >> >> Try it with: >> >> � � 2.3 * 5.12 � = 11.776 >> � � 2.5 * 4.123 �= 11.7875 >> > Well here you imply that all numbers are exact. Above you implied a > precision given by the number of digits presented (see "significance > arithmetic" linked to above). What are we talking about? Read again my message. Hint: there are two parts. -- __Pascal Bourguignon__ http://www.informatimago.com/
From: Vend on 25 May 2010 04:36 On 23 Mag, 01:13, Þorne <ego...(a)gmail.com> wrote: > On May 22, 4:19 pm, "Captain Obvious" <udode...(a)users.sourceforge.net> > wrote: > > > This is the behaviour of IEEE floating point numbers, it is not in any way > > specific to Common Lisp. > > Most programming languages use IEEE floating point numbers, and all they > > suffer from this kind of inexact results because it is in the very nature of > > these floating point numbers. > > > But Common Lisp also supports exact rational numbers, so you can write: > > > (- 343 27315/100) => 1397/20 > > > Does this need any explanation? > > Probably. :-) But leaving that aside, my actual practical concern at > the moment is in the possibility of introducing forms of imprecision > that increase each time some running value is subjected to a new > operation. You may use algorithms that try to avoid that: http://en.wikipedia.org/wiki/Numerical_analysis > Given unlimited space and time (which I effectively have) > is it valid as a rule of thumb to say, "never use a float when a > rational will do?" If I wanted to write a bunch of mathematical > functions, would the right approach then be to use rationals as > constant values? If you really don't care about time and space yes, but keep in mind that some mathematical functions and constant are intrinsecally irrational, hence even representing then with rational would result in approximation errors that may propagate. > 273.15, for example, is the difference between degrees Celsius and > Kelvin. Would I be better doing say, (defconstant +c-to-k+ 27315/100) > ? -- and then only converting to floats on the final output step back > to the user? -- and then also converting input to rationals even if it > won't increase precession, but because it will avoid type contagion > back to floats? (I'm sure you can tell I am a bit over my head here.)
From: Tim Bradshaw on 25 May 2010 04:53
On 2010-05-25 03:02:34 +0100, G�nther Thomsen said: > Well here you imply that all numbers are exact. Above you implied a > precision given by the number of digits presented (see "significance > arithmetic" linked to above). What are we talking about? I'm fairly sure that the standard practice, when writing numbers, is to write the number of places of precision. So if I write "the temperature was 10.3 degrees", what I mean is "when rounded to three places, it is 10.3". If I said "the temperature is 10.3000 degrees" what I mean is "when rounded to *6* places, it is 10.3 degrees", which is a very different statement. Of course computers generally do not understand these subtleties, and are in the process of deforming people's expectations of what things like the above mean in a bad way. |