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


Groups > comp.lang.c > #29213

Re: sscanf question

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: sscanf question
Date 2012-12-13 15:45 -0800
Organization None to speak of
Message-ID <lnmwxhldo9.fsf@nuthaus.mib.org> (permalink)
References (1 earlier) <kade97$lta$2@blue-new.rahul.net> <65867183-4a71-4bdd-8e2d-7301aba0da00@googlegroups.com> <74e828e9-5f56-491d-b010-a5dd0cb4bd13@googlegroups.com> <4be7ce8b-cc02-44e5-8824-246b832fd21f@googlegroups.com> <p_sys.509618$Wc4.426891@fx10.am4>

Show all headers | View raw


"BartC" <bc@freeuk.com> writes:
> "Fred K" <fred.l.kleinschmidt@gmail.com> wrote in message
> news:4be7ce8b-cc02-44e5-8824-246b832fd21f@googlegroups.com...
>> On Thursday, December 13, 2012 2:31:41 PM UTC-8, Fred K wrote:
>>> On Thursday, December 13, 2012 1:40:58 PM UTC-8, Prathamesh Kulkarni
>>> wrote: >
>>> Please could you suggest some better ways than float/double to represent
>>> monetary amounts ? Thanks ;) > > > -- > > -Ed Falk, falk@despams.r.us.com
>>>  > > http://thespamdiaries.blogspot.com/
>>
>> Integer times 100 - i.e., in integer units of the smallest unit in the
>> monetary system.
>>
>> So $1.23 is stored as 123
>>
>> The reason is to avoid roundoff and truncation errors.
>
> You still get problems when doing calculations (there are remainders to take
> care of).
>
> Floating point is workable if you know that an amount might be meant to
> represent a whole number of cents for example. Or you can use floating
> point to represent cents rather than dollars ...

Floating-point can't exactly represent all integer values within its
range -- and operations that yield inexact results generally don't give
any warning about a loss of precision.  If you can guarantee that all
the numbers you'll be dealing with are within the contiguous range of
integers that can be represented exactly, then you'll probably be ok.
But then why use floating-point rather a large integer type in the first
place?

For *serious* financial calculations, there are typically standards that
specify exactly how those calculations are to be carried out.  In such
contexts, you need to find those standards and follow them.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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