Thread: Short program overflow issue?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    10 in base 3 is 3, not 1/3, right?
    Yes, but no need to correct that typo in thought since matsp already did so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Aha, I see.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by matsp View Post
    Actually, floating point numbers consist of a sign, a mantissa (not "base", but number betwee 0.0 and 1.0 expressed in base 2), and an exponent. The actual number is then sign * mantissa * 2 ^ exponent.
    To be absolutely precise, the formula is:
    (-1^sign)*(1. mantissa)*2^(exponent-bais)

    The mantissa thus is a list of significant figures starting at the second. (The first is always 1)
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The original was, however, that floating point numbers are raised to an exponent and can therefore not represent as much as a 128-bit number consisting as two 64-bit parts, one for real and one for float.
    Floating numbers are approximations, and a computer could very well calculate many, many more digits than ones a float or double can represent.
    But even so, I guess that sometimes, there are some numbers that simply cannot be presented by the decimal system.
    The solution for that would be to use exact numbers. It's not impossible for computers to do this, but the IEE standard doesn't specify all of this, so the IEE standard falls short, not the computers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Obviously, there are all sorts of different ways that you CAN represent a number - an in some cases, ALL possible options are approximations (e.g. pi, e, sqrt(2), sqrt(3), and many others).

    Of course, you can express pi or e precisely in the form of pi and e respectively - but if you actually want to eventually get a numeric value, you would have to somehow use an approximation of some sort - it may be 4 billion digits long, but it would still be an approximation.

    Some numbers could of course be described as fractions (e.g. 1/10, 1/3, and so on) - and you could create a math library that multiplies 1/10 * 2/3 and comes up with 2/30, etc. But again, if you want a single numeric answer, it would have to be approximated.

    For most things, the 15-18 digits you can get out of a double is perfectly adequate (in fact, most things are probably precise enough with floats). For example, if you want to figure out if a wall being hit by a car at x meter per second, then 3-4 places would certainly be sufficient to know whether it does or not. If you rely on the fifth place or further down, perhaps you should consider making the wall stronger [if that's what you wanted to know, or drive a bit faster if you are doing a ram-raid]

    If you want more precision, then use GMP or some other multiprecision library.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    The original was, however, that floating point numbers are raised to an exponent and can therefore not represent as much as a 128-bit number consisting as two 64-bit parts, one for real and one for float.
    You mean like a 128 fixed point number with 2^-64 precision?

    Floating numbers are approximations, and a computer could very well calculate many, many more digits than ones a float or double can represent.
    But even so, I guess that sometimes, there are some numbers that simply cannot be presented by the decimal system.
    The solution for that would be to use exact numbers. It's not impossible for computers to do this, but the IEE standard doesn't specify all of this, so the IEE standard falls short, not the computers.
    Sure. A variable width number format is possible, one that stores any arbitrary constant arithmetic expression.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by King Mir View Post
    You mean like a 128 fixed point number with 2^-64 precision?
    I was maybe hinting at:
    11111.22222
    (Real) (Float)
    To separate parts, each stored as two 64-bit numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I was maybe hinting at:
    11111.22222
    (Real) (Float)
    To separate parts, each stored as two 64-bit numbers.
    That would be a 128-bit fixed point number with 64-bit integer and 64-bit fraction part.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. short int vs int
    By time789 in forum C Programming
    Replies: 3
    Last Post: 08-05-2007, 09:37 AM
  4. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM