From: "Michael W. Ryder" <_mwryder@...> Date: 2007-06-22T11:20:08+09:00 Subject: Re: Ruby doesn't know how to multiply Rob Biedenharn wrote: > On Jun 21, 2007, at 8:48 PM, Gregory Brown wrote: >> On 6/21/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote: >>> A simple way to get the correct figure is to do what is used often with >>> currency to prevent these types of errors. If I enter 'puts >>> 1495.0*6.0/1000.0' I get 8.97. The only thing necessary is to keep >>> track of the number of decimal points. >> >> Much better is to store the numbers as integers and convert to >> decimal, guaranteeing zero loss. > > Which are just different ways of saying "fixed point numbers" that matz > had at the top of his list. Believe me when I tell you that dealing > with fixed point can be challenging. I worked on part of an air traffic > control system about 20 years ago that used 16-bit values where the LSB > for a distance value represented 1/256nm (nautical mile). There was no > floating-point co-processor in the M68000-based systems and the C code > was compiled with a special, non-standard compiler that would do things > like (16-bits * 16-bits)/16-bits was assumed to produce a 16-bit result > unless you had a typecast to indicate otherwise. > I think we have a difference of understanding of what fixed point means. I look at fixed point numbers meaning something like 1057 means 10.57 in the real world. I was thinking more of something that shifted the floating point number to remove the fractions and later replaced the decimal point in the correct position, which I do not consider to be fixed point math. My simple calculator is more precise for some math operations than my expensive PC. Maybe we need to implement something like General Decimal Arithmetic which is available for C++ and Java at http://www2.hursley.ibm.com/decimal/ in Ruby. It may be as simple as writing wrappers for the operations, I haven't had to time to look closer at the package. > The other way to get truth from 14.95*0.6 == 8.97 is to use less precise > representations than you get with a typical Float. Of course, that's > going to be a problem as the numbers get bigger ;-) > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob@AgileConsultingLLC.com > > >