setting a float to 4 digit decimal precision

This is a discussion on setting a float to 4 digit decimal precision within the C Programming forums, part of the General Programming Boards category; I am dealing with an old language for the front end to a c program. I am running into coredumps ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Lightbulb setting a float to 4 digit decimal precision

    I am dealing with an old language for the front end to a c program. I am running into coredumps when I pass a float that has more than 4 digits after the decimal.

    To get around this, I did the following:
    passRt=(int)(combRt*10000)

    where passRt is an int being passed back to the calling function, and combRt is a float.

    Then once the passRt value is back in the calling function, I
    LET floatPassRt=passRt/10000
    and end up with a 4 digit precision float.

    Problem is, this is messing with the rounding.


    Without having to write an extensive rounding function (tax logic and all), is there a way that I can actually set a varible to be a 4 digit precision float in C?

    thanks!!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm, I have no idea, but what do you mean by a 4 digit precision float? You mean one that does not track the values of lower decimal significance in the float? Sounds interesting anyway...
    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;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sprintf( buf, "%.4d", myFloat );
    myFloat = atof( buf );

    That might be what you want.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No, quzah, I think he really means a truncated float.
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Truncated so that it will only accept .0000 decimal points, and nothing beyond that? No, no way to do that with a standard float. You could probably fake it in C++ by making a class...

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Perhaps not tho. Think of it. If you somehow shifted the information aspect of a number to higher orders of value, you might have a float that almost reaches past an unsigned long in largeness but yet keeps track of only 4 orders of decimal precision...iffy but not impossible i think.
    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;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In either case though, you're not dealing with a standard 'float' or 'double'. It'd be something you made to do it. So with standard variables, no, I don't think you can. Floats are stored wierdly in memory. Not the same as standard integers and what not.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Unregistered
    Guest
    Agree with you there.

  9. #9
    Unregistered
    Guest
    thanks, guys, I ended up shifting 4 digits, passing as an int, then shifting back. so though its a normal float, it only has four digits that aren't designed to be zero. thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting Precision Confusion
    By dnguyen1022 in forum C++ Programming
    Replies: 11
    Last Post: 01-14-2009, 09:38 AM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 02:41 PM
  5. Setting decimal precision in fstream
    By CodeMonkey in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2003, 05:08 PM

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