Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #29229

Re: sscanf question

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.lang.c
Subject Re: sscanf question
Date 2012-12-14 12:19 +0000
Organization Aioe.org NNTP Server
Message-ID <kaf5ge$ppe$1@speranza.aioe.org> (permalink)
References (5 earlier) <p_sys.509618$Wc4.426891@fx10.am4> <lnmwxhldo9.fsf@nuthaus.mib.org> <hfwys.1119952$Ol2.766606@fx25.am4> <kae67t$isr$1@speranza.aioe.org> <IpEys.510821$Wc4.18818@fx10.am4>

Show all headers | View raw


BartC <bc@freeuk.com> wrote:

(snip, someone wrote)
>>> But if you then have to calculate 15.9% tax on $53.49, it's a lot
>>> easier with floating point.

(then I wrote)
>> Take 5349 (cents) multiply by 159 and then divide by 1000 to get the
>> truncated tax in cents. But with tax, you have to watch the rounding.
>> If you want the rounded value, add 500 before dividing by 1000.
 
> OK, by using 'special methods' as I mentioned. You've now also introduced 
> the arbitrary figures 10 (10*15.9), 500 and 1000, and presumably need to 
> impose some arbitrary convention as to how percentage values are going to be 
> represented in the program (I guess you can't just have 15.9 or 0.159).

Well, you can do it in fixed point decimal just fine. If you write
it in PL/I, instead of C, 15.9 is FIXED DECIMAL(3,1), that is, three
digits with one after the decimal point. If you multiply, then the
compiler knows what you mean and does it all for you, in fixed point.

They added complex numbers to C, yet not many people use them. Maybe
next we can have fixed decimal to make these calculations easier.
 
> At this level of program, just using floating point is a lot easier! You 
> just need to round to a cent at every stage, taking care when values are 
> negative; one little function.
 
>> With floating point, the intermediate values might be rounded.
 
> Do you have a calculation where that rounding (which will be to the 
> low-order bits of the representation) will actually give a different result 
> to doing the whole thing with integers? (And with ordinary amounts that are 
> likely to be encountered.)

With interest rates, it might be unusual, but, as previously mentioned,
exchange rates often go to four decimal places. It won't take such big
values to get rounding errors in that case. How long will it take you
to show that they don't occur? 

You might look at: http://dl.acm.org/citation.cfm?id=221334
 
>> Multiply be the value with four decimal places, after moving the decimal
>> point to the right four places, then divide by 10000. Not hard at all.
 
> No. Waste of time inventing floating point, really!

Floating point was invented for scientific problems that range over
many orders of magnitude, and have inherent relative uncertainty.
That is, the uncertainty scales with the value.

In finance and typesetting, the uncertainty (cents or pixels) is fixed,
independent of the size of the values. In the case of absolute
uncertainty, fixed point arithmetic is a much better choice.

Calculations in exadollars or femtodollars are pretty rare, 
but femtometers and exameters not so rare. (Nuclear physics
and astrophysics, respectively.)

It will take longer to prove that the rounding is right
in floating point than to do it right in fixed point.

-- glen

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

sscanf question "Brian O'Brien" <bjobrien62@gmail.com> - 2012-12-12 13:29 -0800
  Re: sscanf question "BartC" <bc@freeuk.com> - 2012-12-12 21:43 +0000
  Re: sscanf question James Kuyper <jameskuyper@verizon.net> - 2012-12-12 16:52 -0500
  Re: sscanf question Les Cargill <lcargill99@comcast.com> - 2012-12-12 22:14 -0600
    Re: sscanf question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-13 14:03 +0000
  Re: sscanf question falk@rahul.net (Edward A. Falk) - 2012-12-13 20:36 +0000
    Re: sscanf question Prathamesh Kulkarni <bilbotheelffriend@gmail.com> - 2012-12-13 13:40 -0800
      Re: sscanf question Fred K <fred.l.kleinschmidt@gmail.com> - 2012-12-13 14:31 -0800
        Re: sscanf question Fred K <fred.l.kleinschmidt@gmail.com> - 2012-12-13 14:33 -0800
          Re: sscanf question "BartC" <bc@freeuk.com> - 2012-12-13 22:51 +0000
            Re: sscanf question Keith Thompson <kst-u@mib.org> - 2012-12-13 15:45 -0800
              Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-14 00:06 +0000
              Re: sscanf question "BartC" <bc@freeuk.com> - 2012-12-14 02:33 +0000
                Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-14 03:25 +0000
                Re: sscanf question "BartC" <bc@freeuk.com> - 2012-12-14 11:50 +0000
                Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-14 12:19 +0000
                Re: sscanf question "BartC" <bc@freeuk.com> - 2012-12-14 13:38 +0000
                Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-14 20:40 +0000
                Re: sscanf question Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-12-16 03:34 -0800
          Re: sscanf question falk@rahul.net (Edward A. Falk) - 2012-12-15 03:28 +0000
            Re: sscanf question Les Cargill <lcargill99@comcast.com> - 2012-12-14 22:41 -0600
              Re: sscanf question Keith Thompson <kst-u@mib.org> - 2012-12-15 01:25 -0800
                Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-15 10:26 +0000
                Re: sscanf question Les Cargill <lcargill99@comcast.com> - 2012-12-15 13:24 -0600
                Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-15 20:15 +0000
                Re: sscanf question Les Cargill <lcargill99@comcast.com> - 2012-12-15 15:33 -0600
                [OT] Re: sscanf question Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-15 17:49 -0500
                Re: [OT] Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-16 00:16 +0000
            Re: sscanf question Barry Schwarz <schwarzb@dqel.com> - 2012-12-14 21:28 -0800
            Re: sscanf question glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-15 08:17 +0000
      Re: sscanf question Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-13 18:22 -0500
  Re: sscanf question "Brian O'Brien" <bjobrien62@gmail.com> - 2012-12-13 21:13 -0800
    Re: sscanf question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-14 12:25 +0000

csiph-web