Thread: Does C++ have instructions to allow extended precision in floating point operations?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    Does C++ have instructions to allow extended precision in floating point operations?

    I understand that computers use 'floating point representation' to represent fractions and very large numbers. This form of storage is vulnerable to all kinds of round off and significant figure errors.

    One solution is 'extended precision' where the size of the word storing the floating point representation is doubled. I've read math programs like MATLAB give the user the options to use extended precision.

    Does C++ have a keyword or function that instructs the computer to use extended precisions?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ has float, double and long double floating types.
    Each should, if you are lucky, be larger than the previous, meaning it can hold larger numbers and better precision. But it's also up to the implementation and platform to decide what size they are.
    If that won't do (which, IMHO, it does not), you can always implement your own class that fits the bill. There should be pre-developed such classes out on the web.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suspect that you are talking about double. but of course since it is still a floating point representation, the problems remain. If you are talking about fixed point, then that would be different.
    Last edited by laserlight; 07-15-2009 at 09:59 AM.
    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

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I understand that computers use 'floating point representation' to represent fractions and very large numbers. This form of storage is vulnerable to all kinds of round off and significant figure errors.

    Floating-point is a way of representing *approximations* of fractional values. Rounding errors and the like are inevitable.

    >> One solution is 'extended precision' where the size of the word storing the floating point representation is doubled. I've read math programs like MATLAB give the user the options to use extended precision.

    That's not a solution. Given a sufficiently large or small number it too will exhibit rounding errors.

    >> Does C++ have a keyword or function that instructs the computer to use extended precisions?

    Most systems these days at the very least support 4-byte and 8-byte floating point numbers, and these would be mapped to C++'s 'float' and 'double' types, respectively. If you're system supports larger formats, you'd probably need to write some assembly to access it.

    Having said that, if you wanted an even larger number you could of course implement it in software, and if you wanted to do it in a way that wouldn't accumulate errors you could simply store the values as pure fractions (numerator/denominator). Google big-float library or such.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    57
    Yes, I'd forgotton about large data types. Doubles or larger types probably automatically implement 'extended precision'.

    Thanks for pointing out to me the possible software solutions. I should have known all those brains out there would have fixed these problems long ago. I did not realize, however, that the balky computer hardware was so flexible and responsive to these solutions.

    I was concerned about the computer hardware, but it looks like with so many good software solutions I think I can just put in the latest hardware, and not worry about any special optimization.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In the end, it's just bits for the computer.
    It's true, however, that hardware do not have native support for larger floating types, which makes software solutions "expensive" in terms of processor utilization.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. Floating point exceptions
    By Boris in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2001, 08:32 AM
  4. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM