Thread: Value assigned to variable is different than what is displayed when printing variable

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    Value assigned to variable is different than what is displayed when printing variable

    Hi all,
    I assign a value to a variable, i.e. float t=263.15; When I then print this variable, it is displayed as 263.149994. How can I declare/assign this variable differently so that it is not altered as such. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Print it to 2 decimal places

    The internal representation of the number is different from 263.15, and you cannot do much about that other than say, use a fixed point arithmetic library.
    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

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Unfortunately the variable must exactly equal what I assign to it, otherwise the function that I call will not continue.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bkms
    Unfortunately the variable must exactly equal what I assign to it, otherwise the function that I call will not continue.
    In that case, you should use an integer, not a floating point variable, or use a fixed point arithmetic library. If you cannot change this, then I'm afraid that the function that you are trying to call is broken by design.
    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

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by bkms View Post
    Unfortunately the variable must exactly equal what I assign to it, otherwise the function that I call will not continue.
    You have a function that will refuse to execute unless you pass it the specific value 263.15?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please post some code so we can see the right way to help for your situation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    0.15 has an infinite expansion in binary. floats (and doubles) has a finite representation. If you need perfect representation of two decimal points, that's actually fixed-point, not floating-point, like laserlight said. So if you're representing money to the penny, for example, you would store it as pennies, 26315 pennies instead of 263.15 dollars. The only problem is printing it out and reading it in, but that's not too difficult.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    laserlight, I agree that the function is broken by design, since it requires an exact value to execute. I have found a temporary fix... instead of assigning the value within the program, I added some code so that 263.15 is scanned in from the keyboard when executing, and this works for now. Thanks, all, for your help.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    This is probably another (float) vs. (double) issue. Why people still use (float) for anything is a mystery to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accepting a Variable in C without getting it displayed
    By Diwaker in forum C Programming
    Replies: 2
    Last Post: 01-23-2009, 01:03 PM
  2. Printing a variable's name.
    By BrokenShots in forum C Programming
    Replies: 9
    Last Post: 03-12-2008, 03:13 AM
  3. Wrong variable value assigned
    By drag0n69 in forum C Programming
    Replies: 29
    Last Post: 02-29-2008, 03:49 PM
  4. Replies: 20
    Last Post: 11-12-2005, 03:10 PM
  5. Edit a character string with an assigned variable
    By jeffdavis_99 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2005, 10:54 AM