Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-12-2011, 05:47 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default Float problem?

Hi all,

I'm trying to create a unit converter, one of the things it does is temperature conversion.

So the formula for farenheit is f=(c*(9/5))+32

If I run my code with a value of 12, i get 44, however when I do it in spotlight calculator or in the apple converter I get 53.6.

My problem, I have now worked out after splitting up the formula and breakpointing it, is that it says 9/5 = 1, whereas it clearly equals 1.8. For some reason my floats are being rounded (or floored in this case)?? - Should I be using a double or something??

here is my method (split up for debug purposes):

Code:
- (float)celsiusToFarenheit:(float)c {
    float divider = 9/5;
    float firstPart = c*divider;
    float ans = firstPart+32;
    return ans;
}
Thanks in advance,

Alex

EDIT: Sorry have now solved it requires me to put 9.0/5.0 instead of 9/5

Last edited by lexy0202; 06-12-2011 at 05:51 AM.
lexy0202 is offline   Reply With Quote
Old 06-12-2011, 09:51 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

9 and 5 are integers so / is integer division. Try 9.0f/5.0f. Also, if you are hardcoding values for the division, why do it at all, why don't you just hardcode 1.8?
baja_yu is offline   Reply With Quote
Old 06-12-2011, 09:53 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: West Yorkshire, UK
Posts: 63
JohnnyC is on a distinguished road
Default

Does changing 9/5 to 9.0f/5.0f help?

*edit, beaten to it
JohnnyC is offline   Reply With Quote
Reply

Bookmarks

Tags
conversion, double, float, math, rounding

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 316
6 members and 310 guests
chemistry, Dnnake, iOS.Lover, lendo, Leslie80, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0