From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a8249faab338064d,start X-Google-Attributes: gid103376,public From: Al Christians Subject: Re: fixed type (delta) in ada Date: 1999/11/26 Message-ID: <383F808E.66464E08@easystreet.com>#1/1 X-Deja-AN: 553558675 Content-Transfer-Encoding: 7bit References: <383f6dbb.22609744@news.tstonramp.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Trace: typ12.nn.bcandid.com 943685510 206.103.35.231 (Sat, 27 Nov 1999 01:51:50 EST) Organization: Trillium Resources Corporation MIME-Version: 1.0 NNTP-Posting-Date: Sat, 27 Nov 1999 01:51:50 EST Newsgroups: comp.lang.ada Date: 1999-11-26T00:00:00+00:00 List-Id: I ran your test with GNAT 3.12p under Windows NT on Pentium Pro. I get the answer = 68.55. Equally no good or even a little worse. Your fixed point numbers are all binary fixed point. This is all according to the book; don't worry. Delta = 0.01 works out to use binary fixed point with 7 bits to the right of the binary point, ie numbers are represented as some number of 128ths; 1/128 is less than 0.01, so these are good to 2 decimal digits to the right of the decimal point. Unfortunately, it looks like your 0.20 value gets represented by GNAT as 25/128, even though 26/128 is a little closer, neither is very close. Whatever Ada you were using is using 26/128 and is a little more accurate than GNAT 3.12p, but there's a way around this either way (below). If I do this: a: constant := 351.0; b: constant := 1.0/5.0; d: constant := a * b; c: constant money := d; Then c comes out with the right value of 70.2. a, b, and d, are each of type universal_real, with beaucoups of significant digits, and therefore c comes out ok. But if I do this: a: constant := 351.0; b: constant := 1.0/5.0; c: constant money := a * b; Then c still comes out 68.55. Looks like the a and b get converted to the money type before they get multiplied. Ouch. But to make money a decimal fixed point type type, do this: ----------------------------------------------------------- with Ada.Text_IO; procedure Money_Test is type money is delta 0.01 digits 10 range 0.0..10_000_000.00; a: money; b: money; c: money; package money_io is new ada.text_io.decimal_io(money); begin a := 351.0; b := 0.20; c := (a * b); money_io.put(c); -- Prints 70.2 end Money_Test; -------------------------------------------------------------- This gives the right answer with no error. The range constraint on the money type is optional; the digits + delta part combined make the numbers act like decimal numbers. Al Nap wrote: > > does anyone know why mutiplication between fixed type in ada produce a > big round off error? > > type money is delta 0.01 range 0.0..100_000_000.00; > a, b, c : money; > > package money_io is new ada.text_io.fixed_io(money); > > a := 351.00; > b := 0.20; > c := a * b; > > money_io.put(c); > > -- the output should be 70.2 but it prints 71.3 instead. > -- this is so frustrating. typecasting (back to float) doesn't help > either. > > Thanks, > Nap