Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #12260
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Fixed-point arithmetic library |
| Date | 2012-02-23 01:05 +0000 |
| Organization | UK Free Software Network |
| Message-ID | <ji43dm$j5o$2@localhost.localdomain> (permalink) |
| References | <alpine.DEB.2.00.1202222012550.5334@urchin.earth.li> <9ql7jpF3mnU1@mid.individual.net> <nksak719f3qtnogigv4cunsurbv0ti1rk7@4ax.com> |
On Wed, 22 Feb 2012 14:59:12 -0800, Gene Wirchenko wrote:
> On Wed, 22 Feb 2012 23:13:06 +0100, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>
>>On 22.02.2012 22:39, Tom Anderson wrote:
>>> Evening all,
>>>
>>> I would quite like to represent some numbers in fixed point, and do
>>> arithmetic with them.
>>
>>Fixed point math is susceptible to precision issues which can be more
>>severe than those of float and double: 0.01 * 0.2 -> 0.00
>
> Only if the target precision does not have enough decimal places.
>
> I have been working with fixed-point arithmetic in JavaScript so
> that I can add dollar amounts exactly. Maybe OP has something similar
> in mind, though with the number of digits of precision that he wants
> before the decimal point, I hope it is not currency-related.
>
Not necessarily. BigDecimal has few surprises, but then its not fixed
decimal. The only fixed decimal arithmetic I've done in anger was in
Cobol, which will merrily do the following (Identification and
Environment divisions omitted for brevity).
DATA DIVISION.
WORKING-STORAGE.
01 FIXED-DECIMAL-FIELDS.
05 FD-VALUE PIC S9(6)v9(6) COMP SYNC.
05 FD-DIVISOR PIC S9(6)v9(6) COMP SYNC.
05 FD-RESULT PIC S9(6)v9(6) COMP SYNC.
05 FD-REMAINDER PIC S9(6)V9(6) COMP SYNC.
01 DISPLAY-LINE.
05 DL-VALUE PIC -(6)V.9(6).
05 FILLER PIC X() VALUE " / ".
05 DL-DIVISOR PIC -(6)V.9(6).
05 FILLER PIC X() VALUE " = ".
05 DL-RESULT PIC -(6)V.9(6).
05 FILLER PIC X() VALUE " REMAINDER ".
05 DL-REMAINDER PIC -(6)V.9(6).
PROCEDURE DIVISION.
MAIN SECTION.
M-1.
MOVE 0.8 TO FD-VALUE DL-VALUE.
MOVE 2.0 TO FD-DIVISOR DL-DIVISOR.
DIVIDE FD-VALUE BY FD-DIVISOR GIVING FD-RESULT REMAINDER FD-REMAINDER.
MOVE FD-RESULT TO DL-RESULT.
MOVE FD-REMAINDER TO DL-REMAINDER.
DISPLAY DISPLAY-LINE.
STOP RUN.
This would display
" 0.800000 / 2.000000 = 0.000000 REMAINDER 0.800000"
because, of course, it is exactly equivalent to dividing 800000 by
2000000 and then adding decimal points in their correct fixed positions.
Is that what you want or is BigDecimal doing the right thing for your
problem space?
Apologies for not showing a Cobol SSCE, but I don't have a COBOL compiler
available to check it.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-22 21:39 +0000
Re: Fixed-point arithmetic library Jan Burse <janburse@fastmail.fm> - 2012-02-22 22:59 +0100
Re: Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-23 21:15 +0000
Re: Fixed-point arithmetic library markspace <-@.> - 2012-02-23 13:53 -0800
Re: Fixed-point arithmetic library Robert Klemme <shortcutter@googlemail.com> - 2012-02-23 23:21 +0100
Re: Fixed-point arithmetic library Jan Burse <janburse@fastmail.fm> - 2012-02-24 00:02 +0100
Re: Fixed-point arithmetic library Robert Klemme <shortcutter@googlemail.com> - 2012-02-22 23:13 +0100
Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-22 14:59 -0800
Re: Fixed-point arithmetic library Martin Gregorie <martin@address-in-sig.invalid> - 2012-02-23 01:05 +0000
Re: Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-23 21:21 +0000
Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-23 13:49 -0800
Re: Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-23 21:16 +0000
Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-23 14:03 -0800
Re: Fixed-point arithmetic library Lew <noone@lewscanon.com> - 2012-02-23 14:42 -0800
Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-23 15:08 -0800
Re: Fixed-point arithmetic library Martin Gregorie <martin@address-in-sig.invalid> - 2012-02-23 23:05 +0000
Re: Fixed-point arithmetic library glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-02-27 00:59 +0000
Re: Fixed-point arithmetic library Arne Vajhøj <arne@vajhoej.dk> - 2012-02-26 20:50 -0500
Re: Fixed-point arithmetic library Robert Klemme <shortcutter@googlemail.com> - 2012-02-27 07:32 +0100
Re: Fixed-point arithmetic library Jan Burse <janburse@fastmail.fm> - 2012-02-27 13:46 +0100
Re: Fixed-point arithmetic library Roedy Green <see_website@mindprod.com.invalid> - 2012-02-24 16:26 -0800
csiph-web