Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Float Division Problems (C)
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-24-2005, 03:54 AM PM User | #1 MattJakel Regular Coder   Join Date: Mar 2004 Posts: 138 Thanks: 0 Thanked 0 Times in 0 Posts Float Division Problems (C) I have a segment of code that takes an integral float and divides it by ten however many times it's necessary to make it less than one (therefore moving all digits to the right of the decimal). For some reason, I can't get it to work correctly. Here are two different approaches, neither of which works: Code: ```/*printfs added for testing*/ printf("decval is %f\n", decval); while((decval /= 10) > 0) ; printf("decval is %f\n", decval);``` This one, when tested with a decval of five, prints the following: 5.000000 0.000000 I thought that the assignment in the condition might have been messing things up, so I tried the following: Code: ```/*printfs added for testing*/ printf("decval is %f\n", decval); while((decval /= 10) && decval > 0) ; printf("decval is %f\n", decval);``` This did the exact same thing. I then thought that maybe there was something wrong with float division, so I changed the first printf to print decval/10, which showed the correct answer of 0.500000... but the while loop still changed it to 0.000000. What is it that I'm missing here? I am relatively new to C, but I'm surprised to be having these problems with seemingly simple float division... Thanks, Matt __________________ Linux Distribution Download: \$0 OpenOffice.org: \$0 Mozilla: \$0 Gaim: \$0 GIMP: \$0 Converting to Open Source: Priceless
 01-24-2005, 07:02 AM PM User | #2 aman Regular Coder   Join Date: Oct 2004 Posts: 230 Thanks: 0 Thanked 0 Times in 0 Posts here, try this instead.. Code: ```int main() { float decval = 5.0f; printf("decval is %f\n", decval); while ( (decval = (decval / 10.0f)) > 0 ) { printf("decval is %.64f\n", decval); } return 0; }``` or remove the ';' after your while loop...
01-24-2005, 09:54 AM   PM User | #3
shmoove
Regular Coder

Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
Your condition is wrong.
Quote:
 divides it by ten however many times it's necessary to make it less than one
But instead of having a condition of "keep dividing while greater or equal than one" your condition is "greater than 0".
Try:
Code:
```while((decval /= 10) >= 1)
;```

shmoove

 01-24-2005, 10:18 AM PM User | #4 aman Regular Coder   Join Date: Oct 2004 Posts: 230 Thanks: 0 Thanked 0 Times in 0 Posts ahh I missed that part, good catch shmoove
 01-25-2005, 02:57 AM PM User | #5 MattJakel Regular Coder   Join Date: Mar 2004 Posts: 138 Thanks: 0 Thanked 0 Times in 0 Posts Wow I feel stupid. I can't believe I made such a dumb mistake with a loop! Oh well, thanks for the help! Matt __________________ Linux Distribution Download: \$0 OpenOffice.org: \$0 Mozilla: \$0 Gaim: \$0 GIMP: \$0 Converting to Open Source: Priceless

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:49 AM.