Amazon.com Widgets Prog2.4 -- adding floats give wrong answer?
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 20, 2013, 04:26:52 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 2
| | |-+  Prog2.4 -- adding floats give wrong answer?
Pages: [1]   Go Down
Print
Author Topic: Prog2.4 -- adding floats give wrong answer?  (Read 658 times)
rue
Jr. Member
**
Posts: 53






« on: April 21, 2012, 07:19:50 PM »

Based on Progr 2.4, I tried using float numbers.


Code: (Objective-C)
float sum;
       
        sum = 50.5 + 25.12;
        NSLog(@"The sum of 50.5 and 25.12 is %f",sum);

But Xcode says the answer is:
The sum of 50.5 and 25.12 is 75.620003

What gives?
Where did the 0.000003 extra value came from?
Logged
rue
Jr. Member
**
Posts: 53






« Reply #1 on: April 21, 2012, 07:24:50 PM »

Okay, found the answer to my question.

This one works. i.e. using double instead of float.
The question is why?

Code: (Objective-C)
   double sum;
       
        sum = 50.5 + 25.12;
        NSLog(@"The sum of 50.5 and 25.12 is %f",sum);


 The sum of 50.5 and 25.12 is 75.620000
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.