List Info

Thread: "ocaml_beginners"::[] Rounding




"ocaml_beginners"::[] Rounding
user name
2007-01-06 11:29:08

Is there a function for rounding floats?

Thanks

hs

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Rounding
user name
2007-01-06 13:18:51

> Is there a function for rounding floats?
>

int_of_float rounds down.

# int_of_float 3.0;;
- : int = 3
# int_of_float 3.4;;
- : int = 3
# int_of_float 3.5;;
- : int = 3
# int_of_float 3.6;;
- : int = 3

and for standard rounding...

# let round x =
int_of_float (x +. 0.5);;
val round : float -> int = <fun>;
# round 3.4;;
- : int = 3
# round 3.5;;
- : int = 4
# round 3.6;;
- : int = 4

or if you want the rounded result to still be a float...

# let round_float x =
float_of_int (round x);;
val round_float : float -> float = <fun>;
# round_float 3.4;;
- : float = 3.
# round_float 3.5;;
- : float = 4.
# round_float 3.6;;
- : float = 4.

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Rounding
user name
2007-01-06 14:24:17

Thanks Eric,

I assume my request wasn't precise enough. I want to round floats to
a certain accuracity, e.g. up to two digits after the point. Like
1.875623532 shall be rounded to 1.88 and 1.99996343784 shall be
rounded to 2.00. is that possible?

hs

Am 2007-01-06 um 14:18 schrieb Eric Lavigne:

>> Is there a function for rounding floats?
&gt;>
&gt;
> int_of_float rounds down.
>;
> # int_of_float 3.0;;
>; - : int = 3
> # int_of_float 3.4;;
>; - : int = 3
> # int_of_float 3.5;;
>; - : int = 3
> # int_of_float 3.6;;
>; - : int = 3
>
&gt; and for standard rounding...
>
&gt; # let round x =
> int_of_float (x +. 0.5);;
&gt; val round : float -> int = <fun>;
> # round 3.4;;
>; - : int = 3
> # round 3.5;;
>; - : int = 4
> # round 3.6;;
>; - : int = 4
>
&gt; or if you want the rounded result to still be a float...
>
> # let round_float x =
> float_of_int (round x);;
> val round_float : float -> float = <fun>;
> # round_float 3.4;;
>; - : float = 3.
> # round_float 3.5;;
>; - : float = 4.
> # round_float 3.6;;
>; - : float = 4.
>
>
> Archives up to November 11, 2006 are also downloadable at http://
> www.connettivo.net/cntprojects/ocaml_beginners/
>; The archives of the very official ocaml list (the seniors' one) can
> be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames
> etc.
> Yahoo! Groups Links
>;
>
&gt;

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Rounding
user name
2007-01-06 15:03:37

> Thanks Eric,
>;
> I assume my request wasn't precise enough. I want to round floats to
> a certain accuracity, e.g. up to two digits after the point. Like
> 1.875623532 shall be rounded to 1.88 and 1.99996343784 shall be
> rounded to 2.00. is that possible?
>

# let precision_round x digits_after_decimal =
let multiplier = 10. ** (float_of_int digits_after_decimal) in
(float_of_int (int_of_float ((x *. multiplier) +. 0.5))) /. multiplier;;
val precision_round : float -> int -> float = <fun>;
# precision_round 1.23456789 3;;
- : float = 1.235

&gt; hs
>
> Am 2007-01-06 um 14:18 schrieb Eric Lavigne:
>
> >> Is there a function for rounding floats?
&gt; >>
> >
> > int_of_float rounds down.
>; >
> > # int_of_float 3.0;;
>; > - : int = 3
> > # int_of_float 3.4;;
>; > - : int = 3
> > # int_of_float 3.5;;
>; > - : int = 3
> > # int_of_float 3.6;;
>; > - : int = 3
> >
> > and for standard rounding...
> >
> > # let round x =
> > int_of_float (x +. 0.5);;
&gt; > val round : float -> int = <fun>;
> > # round 3.4;;
>; > - : int = 3
> > # round 3.5;;
>; > - : int = 4
> > # round 3.6;;
>; > - : int = 4
> >
> > or if you want the rounded result to still be a float...
> >
> > # let round_float x =
> > float_of_int (round x);;
> > val round_float : float -> float = <fun>;
> > # round_float 3.4;;
>; > - : float = 3.
> > # round_float 3.5;;
>; > - : float = 4.
> > # round_float 3.6;;
>; > - : float = 4.
> >
> >

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Rounding
user name
2007-01-06 18:54:48

2007/1/6, Eric Lavigne < lavigne.eric%40gmail.com">lavigne.ericgmail.com>:
>; > Thanks Eric,
>; >
> > I assume my request wasn't precise enough. I want to round floats to
> > a certain accuracity, e.g. up to two digits after the point. Like
> > 1.875623532 shall be rounded to 1.88 and 1.99996343784 shall be
> > rounded to 2.00. is that possible?
> >
>
> # let precision_round x digits_after_decimal =
> let multiplier = 10. ** (float_of_int digits_after_decimal) in
> (float_of_int (int_of_float ((x *. multiplier) +. 0.5))) /. multiplier;;

you can also use ceil :
let precision_round x digits_after_decimal =
let multiplier = 10. ** (float_of_int digits_after_decimal) in
(ceil ((x *. multiplier) +. 0.5))) /. multiplier;;

look at http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#6_Floatingpointarithmetic
for more floating point function.

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Rounding
user name
2007-01-06 22:58:34

On Sat, 6 Jan 2007, Remi Vanicat wrote:

> 2007/1/6, Eric Lavigne < lavigne.eric%40gmail.com">lavigne.ericgmail.com>:
>;>> Thanks Eric,
>;>>
>>>; I assume my request wasn't precise enough. I want to round floats to
>&gt;> a certain accuracity, e.g. up to two digits after the point. Like
>>> 1.875623532 shall be rounded to 1.88 and 1.99996343784 shall be
>&gt;> rounded to 2.00. is that possible?
>>&gt;
>>;
>> # let precision_round x digits_after_decimal =
>>; let multiplier = 10. ** (float_of_int digits_after_decimal) in
>&gt; (float_of_int (int_of_float ((x *. multiplier) +. 0.5))) /. multiplier;;
>
&gt; you can also use ceil :
> let precision_round x digits_after_decimal =
> let multiplier = 10. ** (float_of_int digits_after_decimal) in
> (ceil ((x *. multiplier) +. 0.5))) /. multiplier;;
>
&gt; look at http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#6_Floatingpointarithmetic
> for more floating point function.

Beware of sign issues with ceil or int_of_float/truncate since
ceil (-.x) <> -. ceil x

--
Martin Jambon
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Rounding
user name
2007-01-06 23:11:24

On Sat, 6 Jan 2007, Martin Jambon wrote:

> On Sat, 6 Jan 2007, Remi Vanicat wrote:
&gt;
>>; 2007/1/6, Eric Lavigne < lavigne.eric%40gmail.com">lavigne.ericgmail.com>:
>;>>&gt; Thanks Eric,
>;>>&gt;
>>;>> I assume my request wasn't precise enough. I want to round floats to
>&gt;>> a certain accuracity, e.g. up to two digits after the point. Like
>>>>; 1.875623532 shall be rounded to 1.88 and 1.99996343784 shall be
>&gt;>> rounded to 2.00. is that possible?
>>&gt;>
>;>>
>>>; # let precision_round x digits_after_decimal =
>>;> let multiplier = 10. ** (float_of_int digits_after_decimal) in
>&gt;> (float_of_int (int_of_float ((x *. multiplier) +. 0.5))) /. multiplier;;
>>;
>> you can also use ceil :
>>; let precision_round x digits_after_decimal =
>>; let multiplier = 10. ** (float_of_int digits_after_decimal) in
>&gt; (ceil ((x *. multiplier) +. 0.5))) /. multiplier;;
>>;
>> look at http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#6_Floatingpointarithmetic
>> for more floating point function.
>
> Beware of sign issues with ceil or int_of_float/truncate since
>; ceil (-.x) <> -. ceil x

Arghhh, no, not int_of_float or truncate.

--
Martin Jambon
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Rounding
user name
2007-01-07 02:07:06

> >>&gt; I assume my request wasn't precise enough. I want to round floats to
> >>&gt; a certain accuracity, e.g. up to two digits after the point. Like
> >>&gt; 1.875623532 shall be rounded to 1.88 and 1.99996343784 shall be
> >>&gt; rounded to 2.00. is that possible?
> >>
> >> # let precision_round x digits_after_decimal =
> >> let multiplier = 10. ** (float_of_int digits_after_decimal) in
> >> (float_of_int (int_of_float ((x *. multiplier) +. 0.5))) /. multiplier;;
> >
> > you can also use ceil :
> > let precision_round x digits_after_decimal =
> > let multiplier = 10. ** (float_of_int digits_after_decimal) in
> > (ceil ((x *. multiplier) +. 0.5))) /. multiplier;;
> >
> > look at http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#6_Floatingpointarithmetic
> > for more floating point function.
>
> Beware of sign issues with ceil or int_of_float/truncate since
>; ceil (-.x) <> -. ceil x

My int_of_float version is rather long, and also always rounds up for
negative numbers. It looks to me like Remi's ceil version always
rounds up. It seems that floor always rounds down, regardless of
sign, so adding 0.5 and then flooring produces correct rounding
behavior. Maybe the following version is correct?

let precision_round x digits_after_decimal =
let multiplier = 10. ** (float_of_int digits_after_decimal) in
(floor ((x *. multiplier) +. 0.5)) /. multiplier;;

__._,_.___
.

__,_._,___
[1-8]

about | contact  Other archives ( Real Estate discussion Medical topics )