/    Sign up×
Community /Pin to ProfileBookmark

simplify fraction

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?

Thanks,

gecko

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@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]
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@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?
Copy linkTweet thisAlerts:
@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. ?
Copy linkTweet thisAlerts:
@ssystemsNov 11.2009 — In any case, I think we've given the OP plenty of ammo now to come up with whatever solution he/she needs. ?[/QUOTE]

Totally.
×

Success!

Help @gecko91 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...