Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Percentages to Fractions

by David Caughell (Monk)
on Feb 03, 2004 at 22:05 UTC ( [id://326325]=note: print w/replies, xml ) Need Help??


in reply to Percentages to Fractions

I'm replying to a bunch of people at once here...

Firstly, jmcnamara, I'm not sure exactly how that works, but it looks really neat. OOC, what does the ~~%_ do?

Just a general comment: My goal was to get something that approximates the truth, rather than represents it exactly. I would prefer 0.039 to be given as 1/25, rather than 974/24975, because I'm looking for "how many hands (roughly) out of 25 or less am I going to get what I'm looking for".

My code currently gives it as 1/23, which isn't very good at all, though! :)

I'm booted into Windows (and the downvotes flow in...) right now, so I'll tweak my code in a little bit, but I think that by narrowing the tolerance, I'd get a little closer to 1/25.

I like Roger's error/tolerance checking. It's much more elegant than what I've got. The reduction sub is pretty awesome too. I remember thinking about doing that, but I cheated by starting with the lowest denominators and numerators, and brute-forcing it, instead. That sub would be great to post to snippets though. :)

ysth, that's a sweet, sweet code. :)

Replies are listed 'Best First'.
Re: Re: Percentages to Fractions
by bunnyman (Hermit) on Feb 03, 2004 at 23:38 UTC
    sub fract { %_=(%_,$_[0],$%); ~~%_ }

    This is bogus. $% is a built-in variable meaning "the current page number of the output." You could also write it as $_{$_[0]} = 1; which has a similar effect on the contents of the %_ hash.

    The ~~%_ is the same as scalar(%_) because the ~ is the bitwise not. A hash in scalar context becomes a string representing the fraction of used hash buckets over the unused buckets.

      The $% just there for obfuscation, it could've been mostly any scalar -- that it's a built-in makes no difference -- as you indicated. However, "recent" optimizations involving shared values between keys makes it not generate the wanted result since the value is constant. For that, different values would be preferable, e.g. $_{$_[0]} = keys %_;.

      Not that anyone ever should even think about using this.

      ihb

Re: Re: Percentages to Fractions
by ysth (Canon) on Feb 04, 2004 at 00:08 UTC
    1/26 is closer to .039 than 1/25.

    And thanks.

Re: Re: Percentages to Fractions
by tsee (Curate) on Feb 03, 2004 at 22:11 UTC
    Not that I want to encourage you to use Windows, but there is in fact a perl port for Windows that's really simple to install: http://www.activestate.com/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://326325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-18 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found