I am a high school student who has gotten interested in C++ language. For exercises I try to write programs for lessons that I learn in class (For ex. for dimensional analysis in chemistry I wrote a unit converter). I have decided that I am going to try writing a formula to change from percent composition to the empirical formula (For anyone who doesn't know what that means http://dl.clackamas.cc.or.us/ch104-04/empirica.htm). I'm planning out the logical progression on paper before I start writing code, and the problem I foresee myself having is this:
As soon as you get the ratio, where the lowest one is 1.00, and say the other is 1.33 - How can the computer figure out what to multiply to simplify it? How can you let the computer know "if its about 1/3, then multiply it by 3, if its about 3/4 multiply it by 4, etc.," Because I think it would be improbable to check it against every possible fraction.
TL;DR How do would you write "if it's 0.33, multiply it by 3" and also how would you say if it was 1.01 or something, round it down and count it as one.
0.333333... == 1/3. You're trying to multiply by the reciprocal of this, so would multiply by 3. The reciprocal of any number n is just 1/n. So in this situation, you would multiply it by 1/(1/3) or as the computer knows, 1/.3333...
and also how would you say if it was 1.01 or something, round it down and count it as zero
Why you would you want to round it down? It's not zero, so why treat it as zero?
EDIT:
Link to WolframAlpha: http://wolfr.am/ZdXjr8
Illustrates better what's going on here. As computers are finite, it will have to round off this number. If you more trailing 3s to the calculation, you will see there will be more 0s immediately following the 3. in the answer. After rounding/truncation, you'll be left with 3. This will work for any number you're trying to get like this.
Well if you have something like 1:1.33 you could try simply divide by the lone decimal
1 2
(1 : 1.33) / 0.33
=3 : 3.99
1 2
(1 : 2.25) / 0.25
=4 : 9
and then just round. but if you have anything with multiple ratios (such as 1 : 1.2 : 1.3 that could be problematic... also for things that aren't as nice numbers.
Yes good point. I'm trying something easier to begin with, going from the chemical formula to the percent composition. However, I'm having prolems with this code: