C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-06-2010, 10:04 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 3
Finding decimal places for a division

Im writing a program to do arithmetic with random generated numbers, how do i find the exact decimal placing an answer have after applying division on the 2 generated numbers?

For example 10/4 = 2.5, how would i get the program to tell me its '1' decimal placing?
Decimals is offline   Reply With Quote
Old 02-06-2010, 11:27 AM   #2
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 1,243
Well, first, of course you have to cast the result to a float or a double. Otherwise the result will be an integer, in which case there are no "decimal places".

Next, the computer doesnt store the result as 2.5, so it will never have just "1 decimal place". It would be stored as 2.500... with as many zeros as it can store. When you "printf" the number, what it shows the number as depends on the format of output you use. So you can use "%g" to print the number without trailing zeros, so it would be printed as "2.5" exactly. If you use "%f" it will print some trailing zeros, like "2.500000000". There may be other formatting you can use to get more different output. That wasnt your question, but I add that in the case thats what you meant.

You can look into the modf - C++ Reference function, which splits a number into the integer (whole number) part, and fractional part. So calling it with 2.5 should result in integer 2 and fractional part 0.5. Though this is a similar thing as above, it isnt internally represented as "0.5", but as "0.5.....".

The easiest way to do it, probably is to Cprogramming.com FAQ > Convert an int to a string (char array) (C) [EDIT: of course this example is for "ints", but you should be able to figure out how to convert it to "floats"]. From there, you can find where the decimal place is. Next, you check each character of the string from right to left (i.e. "backwards"). The first non-zero number you see is where the fractional part "ends". Now you know where it starts and ends and can figure out how many numbers in between.

Of course there are other, strictly algebraic (math) ways to do this, but it would be more work (or at least more code). I imagine any efficiency you would get from that wouldnt be worth the additional code.

Last edited by nadroj; 02-06-2010 at 11:32 AM.
nadroj is offline   Reply With Quote
Old 02-06-2010, 11:30 AM   #3
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,131
An easy way would be to convert your answer in float or double to a string and then parse the string, unless your C compiler has <decimal.h> which might indicate your hardware supports native data type formats such as decimal float (or your compiler is able to emulate base 10 floats).
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Old 02-06-2010, 01:01 PM   #4
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 3,135
You could store the values in a fraction class, rather than actually performing the division, and then work out how many times you need to multiply the fraction by 10 for the demoninator to become 1.

However, how many decimal places would you say is in 8/7 ?!
__________________
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"
iMalc is offline   Reply With Quote
Old 02-06-2010, 11:12 PM   #5
Registered User
 
Join Date: Feb 2010
Posts: 3
Hmm, maybe my thought process of my objective is wrong. I want to generate two random numbers for division, however the answer of the division can't be more than two decimal places.

Is there a certain math logic to solve this issue or do we have to manually check the places and regenerate the numbers?
Decimals is offline   Reply With Quote
Old 02-07-2010, 09:32 AM   #6
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,131
If your requirement is that simple, then just use ints. When you generate the numbers, multiply them by 100. Then, do your division and format it so that the last two digits are placed to the right of your inserted decimal point.

For instance
Code:
char mystring[15] = {0} ; 
int a = rand() * 100 ; 
int b = rand() * 100 ; 
int c = a/b ;   
....
// do your magic to format the number to a string with 2 decimal places... 
... 
printf("number is %s\n", mystring) ;
For exampe, if a were 500 (which was 5 * 100) and b were 300, (3 * 100) then c would be 166, or 1.66 after you format it.
__________________
Mac and Windows cross platform programmer. Ruby lover.

Last edited by Dino; 02-07-2010 at 09:36 AM.
Dino is offline   Reply With Quote
Old 02-07-2010, 11:30 AM   #7
Registered User
 
Join Date: Feb 2010
Posts: 3
Thanks for your time, but i believe there may be some misunderstanding somewhere.


Im intending to generate two random numbers, from 0-100, and the division of the numbers cannot be more than 2 decimal places. If there is, the numbers won't be shown and regenerated till the result is less than 2 decimal places.

Hope that makes things clearer.

Example
The 2 random numbers can be 9 and 4 so the result is 2.25. However it can't be 1 and 7 so the results is 0.142857143 and is way more than 2 decimal placing



btw those dism students feel free to say hi lol

Last edited by Decimals; 02-07-2010 at 11:37 AM.
Decimals is offline   Reply With Quote
Old 02-07-2010, 11:41 AM   #8
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 1,243
Dino's hack is pretty clever and is the easiest solution. For numbers between 0-100 with at most 2 decimal places of accuracy, you generate integers from 0-100,000. You then divide this number, as a float, by 100.

Say the number you generate is 12,345. You divide this number (as a float) by 100, giving 123.45.

EDIT: That should be: generate numbers from 0-10,000. So a number 1,234 divided by 100 would be 12.34.

Last edited by nadroj; 02-07-2010 at 03:49 PM.
nadroj is offline   Reply With Quote
Old 02-07-2010, 03:27 PM   #9
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,131
Well, obviously there is an understanding here. Only after the 3rd post did you specify your full requirements about the numbers being between 0 and 100. Therefore, adjust my code like this:
Code:
char mystring[15] = {0} ; 
int a = (rand() % 101) * 100 ;    // corrected - was / 
int b = (rand() % 101) * 100 ;    // corrected - was / - thanks King Mir 
int c = a/b ;   
....
// do your magic to format the number to a string with 2 decimal places... 
... 
printf("number is %s\n", mystring) ;
Note the division by 101. This will give an inclusive range from 0 and 100.

Now, if you are just starting out in C in a course of study, then your assignment might be related to this last week's topic, which could have been floats, or string or number formatting, with pattern strings like "%.2f" or some such random pattern that just sprung out of my head for no particular reason...
__________________
Mac and Windows cross platform programmer. Ruby lover.

Last edited by Dino; 02-07-2010 at 06:58 PM. Reason: typo on modulo
Dino is offline   Reply With Quote
Old 02-07-2010, 06:14 PM   #10
Registered User
 
Join Date: Apr 2006
Posts: 1,337
I'm not sure what Dino's code is supposed to do, but whatever it is, it's not doing it. The result from rand() allways needs to be taken modulo the desired range.

What you need to do is to multiply the numerator by 100. Then check that the new number is divisible by the denominator (using %). If it is, then you know that the quotient can be represented by with two decimal point representation precisely.
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.

Last edited by King Mir; 02-07-2010 at 06:20 PM.
King Mir is offline   Reply With Quote
Old 02-07-2010, 06:57 PM   #11
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,131
Quote:
Originally Posted by King Mir View Post
I'm not sure what Dino's code is supposed to do, but whatever it is, it's not doing it. The result from rand() allways needs to be taken modulo the desired range.

What you need to do is to multiply the numerator by 100. Then check that the new number is divisible by the denominator (using %). If it is, then you know that the quotient can be represented by with two decimal point representation precisely.
Yep - typo - should be modulo. Will fix. thanks.
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal places Gordon Windows Programming 9 06-08-2008 09:04 AM
Decimal places Gordon Windows Programming 4 09-28-2007 10:03 AM
Resource ICONs gbaker Windows Programming 4 12-15-2003 07:18 AM
Altering decimal places of float values read from Binary file Kelly C Programming 10 06-12-2002 03:15 PM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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