Hi, I’m grabbing the exif data from an image with php. For the exposure, I’m getting a fraction like 10/x or 1/x (10/2000, etc). How can simplify this fraction so the numerator is always 1 (1/x). 10/2000 = 1/200?
@NogDogNov 11.2009 — #Assuming it's a string in the form "<numerator>/<divisor>", you could split it on the "/", then divide the divisor by numerator: [code=php] <?php $fraction = "10/1000"; list($num, $div) = explode("/", $fraction, 2); $fraction = "1/" . round((int) $div / (int) $num, 0); echo $fraction; [/code]
@ssystemsNov 11.2009 — #Assuming it's a string in the form "<numerator>/<divisor>", you could split it on the "/", then divide the divisor by numerator: [/QUOTE]
Actually you need to divide both by their GCF. Check this on some techniques to get the GCF. Also if I'm not mistaken exif tags usually come in SRational object right? It should have a function/method there already for simplification. I've been looking for my documentation for that somewhere but can't seem to locate the **** thing.
@NogDogNov 11.2009 — #Actually you need to divide both by their GCF. Check this on some techniques to get the GCF. Also if I'm not mistaken exif tags usually come in SRational object right? It should have a function/method there already for simplification. I've been looking for my documentation for that somewhere but can't seem to locate the **** thing.[/QUOTE]
Well, since the stated goal was to express it as 1/x, you would get this by dividing both the numerator and the denominator by the numerator, since any number divided by itself is 1, and when you divide both by the same value then the resulting fraction is equivalent to the original fraction. Since we already know that the result of dividing the numerator by itself is going to be 1, there's no reason to actually do that portion of the arithmetic. If, however, the stated objective was incomplete and there could be cases where the desire is not to have the numerator set to 1, then your point may be well taken.
@ssystemsNov 11.2009 — #Point taken. I still can't remember any mathematical equation/theorem/corollary/etc. that would satisfy his requirement faithfully. e.g. 2/3 on your equation would yeild 1. Now if he's using this for the latitude/longitude/altitude/zenith/etc. exif tags then that inaccuracy would mean like 5000 mi or more. So the question now is what are you really trying to do gecko?
@NogDogNov 11.2009 — #Point taken. I still can't remember any mathematical equation/theorem/corollary/etc. that would satisfy his requirement faithfully. e.g. 2/3 on your equation would yeild 1. Now if he's using this for the latitude/longitude/altitude/zenith/etc. exif tags then that inaccuracy would mean like 5000 mi or more. So the question now is what are you really trying to do gecko?[/QUOTE]
Valid point, in which case you might need to change the round() to allow for some decimal points, or even add some sort of branching logic to adjust the rounding decimal places depending upon the ratio? In any case, I think we've given the OP plenty of ammo now to come up with whatever solution he/she needs. ?