Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Fraction Maker
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fraction Maker



Jason Bourque wrote on Wed, 20 Oct 2004 09:44:21 -0400:

>
>Does anyone have a Fraction Maker?
>
>I have the larger value 1 and a smaller decimal .66
>
>And I want to get 2/3. I now it will take some rounding.

Maybe that's "unrounding".  ;-)

Here's a hack that's accurate with two decimal places and "convenient"
with three. Thus 0.66 is rendered as "33/50", while 0.666 is "2/3". the
original request was for something that would return 0.66 as "2/3". That
can be achieved by changing the first four numbers in the code below to
0.02, 100.0, 100.0, and 4.0E-4 respectively. (But then it becomes less
satisfying with other numbers. 0.62, for instance, is rendered as 18/29
instead of 31/50.)

  on makeFraction(divd)
    set tolerance to 0.002
    set divd to rnd(divd * 1000.0) / 1000.0
    if (abs(divd - rnd(divd)) <= tolerance) then set tolerance to 4.0E-5

    set d to divd
    set divd to divd + 1.0E-12
    set divr to 1

    repeat while (abs(divd - rnd(divd)) > tolerance)
      set divd to divd + d
      set divr to divr + 1
    end repeat

    return (rnd(divd) as string) & "/" & divr
  end makeFraction

  on rnd(n)
    n div 0.5 - n div 1
  end rnd

  on abs(n)
    if (n < 0) then return -n
    n
  end abs

  display dialog makeFraction(0.666)


NG
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.