$frac()

By Firstmate on May 12, 2009

I don't think it's a too difficult of a script to make, but I'll post it since I've never seen it.
Turns decimals into fractions. yay

Sample output:

$div(.5) -> 1/2
$div(.333) -> 1/3
$div(1.5) -> 3/2
$div(.342857) -> 12/35

A note on the number of significant figures. When doing something like 12/35 where the number of sigfigs is exactly 6, try to use 6. On numbers such as 1/3 where its continuous, it's recommended to use 4.

alias frac {
  var %x 1,%t $mid($1,$pos($1,.,1),0) - 1,%p
  while (%x) {
    %p = $round($calc($v1 * $1),0)
    if ($round($calc($+(%p,/,$v1)),%t) == $1) return $+(%p,/,%x)
    inc %x
  }
}

Comments

Sign in to comment.
knoeki   -  May 16, 2009

Thanks, kinda did in a rush, but that's no excuse :p

sure it is, it just isn't a really good one ;_)

(admittedly, I tend to use it too often as well..)

 Respond  
Firstmate   -  May 13, 2009

Thanks, kinda did in a rush, but that's no excuse :p

 Respond  
WorldDMT   -  May 13, 2009

hi

sorry about my language i don't speak verry well english

no need to "return $false"

u was declared %p local why u retype var %p down? type only %p = ...

the variable %y and %t just for len of number after the point?

put only "%t $mid($1,$pos($1,.,1),0) - 1"

no need $round($1,%t) type only $1 cz the %t is the len of number after the point

so

alias frac {
  var %x 1,%t $mid($1,$pos($1,.,1),0) - 1,%p
  while (%x) {
    %p = $round($calc($v1 * $1),0)
    if ($round($calc($+(%p,/,$v1)),%t) == $1) return $+(%p,/,%x)
    inc %x
  }
}
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.