Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

basic C

Name: Anonymous 2010-11-24 12:17

can you help create a program that will accept an amount and output its denomination.

example

amount: 5670.45

1000: 5
500: 6
200: 0
100: 6
50: 1
20: 1
10: 0
.25: 1
.05: 3

Name: Anonymous 2010-11-25 15:06

>>39
floating point error. Add +0.005 before cast, but that doesn't work for negative values...

Name: Anonymous 2010-11-25 16:16

>>41
Add [arbitrary amount] before cast
Or use nextafter or (*((int*)&number))++

Name: Anonymous 2010-11-25 16:21

>>42
wouldn't that round up?

Name: Anonymous 2010-11-25 16:33

>>43
That wasn't the issue I was addressing

Name: Anonymous 2010-11-25 17:29

>>27
I'm usually as bad as all of us, I just... er... I'm just wearing my Sussman hat?

>>30
Just guessing, but: partly for map/reduce cred. Partly because 5 already has some things named reduce and partly because there is no foldr (so the folding concept isn't so strong.) You can @list.reverse.reduce({...}); instead, and swap $^a and $^b in the closure.

A cute thing about Perl 6 is that map was made a little better, but I use it less often. In many cases where map is appropriate, factoring the problem a little differently without map is just as good or better.

Name: Anonymous 2010-11-26 12:21

>>15
($amt, @dens).reduce: {
    my $x = 0;
    if $^b <= $^a {
        $x = ($a/$b).floor;
        say "$b:\t$x";
    }
    $a - $b * $x;
};


The colon form helps take the pain out of supplying closures to methods.

Name: >>12 2010-11-26 14:23

>>13
I don't really know C. "x = 0.05000; x >= 0.05" being false is bullshit, though. Subtracting in a loop might've made more sense to >>1 and I don't know if C has a divmod function.

Name: Anonymous 2010-11-26 14:49

>>47
Not knowing C isn't your problem. Your problem is not knowing IHBT 754.

Name: Anonymous 2010-11-26 15:20

>>47
Looks like somebody hasn't read their 754.

Name: Anonymous 2011-02-04 19:46

<

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List