How accurate is the following...

This is a discussion on How accurate is the following... within the C Programming forums, part of the General Programming Boards category; Originally Posted by filker0 (Financial programs do not, as a rule, use floating point at all for money calculations or ...

  1. #16
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Quote Originally Posted by filker0
    (Financial programs do not, as a rule, use floating point at all for money calculations or storage; they tend to use fixed point to avoid rouding problems and other boundary conditions.)
    What exactly is meant by fixed point? How do you ensure fixed point calculations in programs?

    Thanks

    Eddie

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by filker0
    quzah seems to take my posts as a personal affront on his person. It is not.
    Oh, yeah, well when you send me an email that says, and I quote:
    Though I'm willing to bet that I'm more experienced with C than you are.
    One has to wonder who's taking what personal. Hmm? Especially considering it came out of the blue, because I apparently commented on some post of yours. And that apprently upset you. I guess no one is allowed to point out your mistakes. Seems like you're continually overcompensating. But we won't go there.

    Quote Originally Posted by filker0
    I was making a clarification on the part of the statement that emeyer made. Because it was neither entirely correct nor entirely descriptive (since the range of the exponent is also larger in an IEEE double vs. an IEEE float), I decided to clarify some things. I don't want to get in to a full dissertation on the various floating point formats, nor the details of floating point manipulation. There have been, in the past, many different floating point representations. I'm sticking with IEEE for this discussion.

    As for reading the standards -- I participated in the creation of the ANSI C89 standard while working at a C compiler company. I have written IEEE, Dec-Vax, and Motorolla "fast" floating point code, in assembler and C. I have copies of the publications in which the format is formalized. I've written the documentation on these floating point representations that shipped with the programmers guide for a commercial compiler.

    And you get a little more than double the precision (53 vs. 24 bits of mantissa), and (because it's exponential) much more than double the range (10 bits vs. 8 bits of exponent) with an IEEE double vs. IEEE float representation.
    So, yeah. Sorry about your penis.

    Quote Originally Posted by filker0
    In making my posting that quzah finds so absurd, I simply wanted to give emeyer a better example of the difference between float and double, and maybe encourage him to be more precise in his language. As a professional programmer, I know how important knowing this sort of knowlege is in avoiding difficult to locate coding errors in scientific and financial programs. (Financial programs do not, as a rule, use floating point at all for money calculations or storage; they tend to use fixed point to avoid rouding problems and other boundary conditions.)

    I wrote my post to help emeyer, not pretend to have a point where I have none. quzah may choose to take issue with what I say; that's his right -- but that does not make my statements wrong.
    I didn't take issue with you. I said it was an accurate statement. It is in fact accurate. Doubles are designed to be bigger than floats. That's fact. That was my point that for some reason you disagreed with it. I guess ... well, see above.

    edit in blue

    Quzah.
    Last edited by quzah; 12-06-2005 at 11:56 AM.
    Hope is the first step on the road to disappointment.

  3. #18
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    Fixed point

    Quote Originally Posted by emeyer
    What exactly is meant by fixed point? How do you ensure fixed point calculations in programs?
    Fixed point is where you have a representation of non-integer values that has a fixed precision. Other than the trivial case of integer math (which is, in a sense, fixed point, since there is no fractional part), fixed point is not directly supported by the standard C language. You can do fixed point many ways, and there are a number of libraries out there that do it different ways. Depending on the range you require (maximum/minimum values) and precision desired, different strategies are used.

    In some cases, a fixed point value is represented by a structure containing a whole and fractional part. In these cases, all arithmetic on fixed point values is done by passing pointers to fixed point objects (structs) (or the structs themselves) to functions that have been defined by the fixed point library you're using -- these libraries enforce specific rounding/truncation rules on division and multiplication. (In C++, I've seen this done with classes and operator overloading, as in this link, but we're talking about C here.)

    Another technique used for fixed point is BCD (Binary Coded Decimal), which predates digital computers. In this representation, four bits are used for each decimal digit. To use this in C, you need a BCD library.

    Another common technique used for fixed point is to use an integer type, such as a long or a long long (64 bits), and have an implied decimal point. Here, you can use the built-in arithmetic in C, but multiplication and division require some fix-up afterwards to keep the decimal point in the right place. There are some issues about how wide the result of a multiply is (eg: to avoid overflow, multiplying two 32 bit fixed point numbers requires 64 bits for the result, then adjust the value to move the decimal point to the right place, then check for result overflow which keeps the fixed-point result from fitting in your normal fixed point representation, and if there is no overflow, just take the lower 32 bits -- what you do with overflow is application or library specific) and things of that sort, and your range will be somewhat limited. An example of this strategy can be found at this Stanford university web page.

    Fixed point is sometimes used for high performance signal processing and graphics engines, since their coordinate space is limited and fixed, "enough precision" is well defined, and floating point operations are expensive; eg: this example from Intel.

    Some special and general purpose processors support native fixed point formats. C compilers for those processors sometimes include extensions to allow C programs to declare and use fixed point variables, but these are non-portable.

    Some programming languages do support built-in fixed point representations, such as PL/1 and Cobol.

    A very good overview and source of fixed-point (and arbitrary precision) information and libraries is at this IBM site.
    Last edited by filker0; 12-06-2005 at 02:05 PM. Reason: Add a very good reference to the topic
    Insert obnoxious but pithy remark here

  4. #19
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Code:
    last_name = "Meyer";
    This isn't right, surely? Even if it had been malloc'd (which it hadn't), you can't do it this way surely?
    MSVC++ 6.0

  5. #20
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Surely you can. last_name is a pointer and you are assigning it the address of the string constant "Meyer". All is kosher as long as you don't try to change the string.

  6. #21
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    If you say so. I know you can initialise string that way, but I was always taught to use strcpy. And I also thought the compiler wouldn't reserve any chars for a string simply by declaring a char*.

    Also, what's the pointer pointing to? It hasn't been malloc'd.
    MSVC++ 6.0

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but I was always taught to use strcpy
    That's if you want a copy of the string.

    >And I also thought the compiler wouldn't reserve any chars for a string simply by declaring a char*.
    It reserves enough memory for a pointer, and then you can point it wherever you want, including to a string literal.

    >Also, what's the pointer pointing to? It hasn't been malloc'd.
    It's pointing to a string literal. There's no need to allocate memory for an existing object.
    My best code is written with the delete key.

  8. #23
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > last_name = "Meyer";
    It's like
    const char anon[] = "Meyer"; last_name = anon;

    Without all the annoyance of having to make up non-existent names for things yourself.
    Storage is reserved and initialised by the compiler, and then assigns the pointer to the start of that storage.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need advice to provide accurate time delay in C
    By kenkoh in forum C Programming
    Replies: 14
    Last Post: 04-30-2008, 11:13 AM
  2. Frustum cull is too accurate
    By VirtualAce in forum Game Programming
    Replies: 12
    Last Post: 01-25-2006, 11:35 AM
  3. accurate CPU temp reading
    By itld in forum Tech Board
    Replies: 20
    Last Post: 01-21-2003, 04:50 PM
  4. How do I create an accurate timer?
    By Quin in forum C Programming
    Replies: 1
    Last Post: 01-06-2002, 01:28 AM
  5. VERY accurate timing
    By Gherkin in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-28-2001, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21