Rounding up in perl

D

David Groff

Is there more than one function for rounding a
number up in perl?
I tried the ceil() function but get an undefined subroutine
message.
 
J

Jürgen Exner

David Groff said:
Is there more than one function for rounding a
number up in perl?
I tried the ceil() function but get an undefined subroutine
message.

See
perldoc -q round
"Does Perl have a round() function? What about ceil() and floor()?"

jue
 
M

Marshall Dudley

David said:
Is there more than one function for rounding a
number up in perl?
I tried the ceil() function but get an undefined subroutine
message.
Simply add 0.5 to the number and round, or convert to an integer then
add 1 or add 1 then convert to an integer.

Marshall
 
J

Jürgen Exner

Marshall Dudley said:
David said:
I tried the ceil() function but get an undefined subroutine
message.
[...] convert to an integer then
add 1 or add 1 then convert to an integer.

Bad idea if the number is a natural number already.

jue
 
S

Scott Bryce

Marshall said:
Simply add 0.5 to the number and round, or convert to an integer then
add 1 or add 1 then convert to an integer.


Neither of these methods is bullet proof. Accuracy of the results will
depend on the number and/or the method used to do the rounding.




use strict;
use warnings;

while (my $i = <DATA>)
{
chomp $i;
my $j = $i + 1;
my $k = $i + 0.5;
print "$i rounds to ", sprintf('%d', $j), ' or ', sprintf('%.0f', $k),
"\n";
}


__DATA__
2
3
 
S

sln

Is there more than one function for rounding a
number up in perl?
I tried the ceil() function but get an undefined subroutine
message.

Here is a Perl ceil/floor equivalent. The first code section
seems to correctly implement floor by taking into account that
int() does not use the sign in its process of rounding. The
second code section with floor, although intuitive is not correct.
For a full proof, all the values between 2.0 - 2.9 should be checked.

sln

# ===========================================
# Correct ceil/floor equavelent
# This floor takes the sign into acount
# when rounding.
# - - - - - - - -
use strict;
use warnings;

printf( "The floor of 2.8 is %f\n", _floor( 2.8 ) );
printf( "The floor of -2.8 is %f\n", _floor( -2.8 ) );

printf( "The ceil of 2.8 is %f\n", _ceil( 2.8 ) );
printf( "The ceil of -2.8 is %f\n", _ceil( -2.8 ) );

sub _ceil {return int(shift()+.5)}

sub _floor {my $y = shift(); return int(($y < 0) ? $y-.5 : $y)}

__END__

The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


# ===========================================
# Correct ceil equavelent, incorrect floor
# Int() rounds down the Absolute value.
# So do not use this floor.
# - - - - - - - -
use strict;
use warnings;

printf( "The floor of 2.8 is %f\n", _floor( 2.8 ) );
printf( "The floor of -2.8 is %f\n", _floor( -2.8 ) );

printf( "The ceil of 2.8 is %f\n", _ceil( 2.8 ) );
printf( "The ceil of -2.8 is %f\n", _ceil( -2.8 ) );

sub _ceil {return int(shift()+.5)}

sub _floor {return int(shift())}

__END__

The floor of 2.8 is 2.000000
The floor of -2.8 is -2.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


==========================================
C - reference code

// crt_floor.c
// This example displays the largest integers
// less than or equal to the floating-point values 2.8
// and -2.8. It then shows the smallest integers greater
// than or equal to 2.8 and -2.8.


#include <math.h>
#include <stdio.h>

int main( void )
{
double y;

y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );

y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}


Output

The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000
 
T

Tad J McClellan

taking into account that
int() does not use the sign in its process of rounding.


That is because int() does not *have* a process of rounding.

int() does not do rounding.
 
S

sln

That is because int() does not *have* a process of rounding.

int() does not do rounding.

Not only does int() round, but there is no way it can return
without rounding.

sln


perlfunc.html
===============================
Numeric functions

...., int, ...

int EXPR

int

Returns the integer portion of EXPR. If EXPR is omitted,
uses $_. You should not use this function for rounding:
one because it truncates towards 0, and two because machine
representations of floating point numbers can sometimes
produce counterintuitive results.
 
U

Uri Guttman

s> Not only does int() round, but there is no way it can return
s> without rounding.

and you determined that from what text in the docs you quote?


s> perlfunc.html
s> ===============================
s> Numeric functions

s> ..., int, ...

s> int EXPR

s> int

s> Returns the integer portion of EXPR. If EXPR is omitted,
s> uses $_. You should not use this function for rounding:
s> one because it truncates towards 0, and two because machine
s> representations of floating point numbers can sometimes
s> produce counterintuitive results.

and where does it say int() does rounding? it even says you should NOT
use it for rounding. truncation (which is what int() does) is not
rounding in any sense of the term rounding. your saying otherwise will
not make it so.

uri
 
S

sln

s> Not only does int() round, but there is no way it can return
s> without rounding.

and you determined that from what text in the docs you quote?


s> perlfunc.html
s> ===============================
s> Numeric functions

s> ..., int, ...

s> int EXPR

s> int

s> Returns the integer portion of EXPR. If EXPR is omitted,
s> uses $_. You should not use this function for rounding:
s> one because it truncates towards 0, and two because machine
s> representations of floating point numbers can sometimes
s> produce counterintuitive results.

and where does it say int() does rounding? it even says you should NOT
use it for rounding. truncation (which is what int() does) is not
rounding in any sense of the term rounding. your saying otherwise will
not make it so.

uri

It says it should not be used for rounding, for TWO reasons.
Thats two references to ROUNDING you say it doesen't use.

sln
 
T

Tim Greer

It says it should not be used for rounding, for TWO reasons.
Thats two references to ROUNDING you say it doesen't use.

sln

Or... it's that it could be used for rounding, but that it's not a good
idea, as you could have unexpected results. I'm sure there are other
functions to misuse just as easily, if you prefer.
 
S

sln

(e-mail address removed) wrote:



Or... it's that it could be used for rounding, but that it's not a good
idea, as you could have unexpected results.
[snip]
And just what is that?

sln
 
S

sln

int() does not do rounding.




int() does do truncating.

truncating is not rounding.

You've confused posts here bud. Your clipping
what the docs? Not my words.

Does it truncate? What does it truncate?
How exactly do you truncate? Please tell us all.
While your at it, tell us what is rounding up and
down and how it relates to Perl.

Then tell us what Perl's int() is good for.

Thanks!

sln
 
S

sln

Good luck with that proof. The set of all numbers x such that
2.0 <= x <= 2.9 is uncountable.

Actually...why don't you stop posting till you complete the proof.

--keith

As a proof, I'm only instred in what happens int the +-.1 range (-+)
of .5, not the whole universe of decimal places to the right.

sln
 
S

sln

As a proof, I'm only instred in what happens int the +-.1 range (-+)
of .5, not the whole universe of decimal places to the right.

sln

Although, margins should be considered a factot in hyperbolic
design considerations, takr it with a grain of salt, add .000000000\
00000000000000000000000000000000000000000000000000000001 and see
how it plays out.

You just never know, you could discover the asteroid that perishes the
earth.

sln
 
T

Tim Greer

(e-mail address removed) wrote:



Or... it's that it could be used for rounding, but that it's not a
good idea, as you could have unexpected results.
[snip]
And just what is that?

sln

It might appear to be working fine and give you the desired/expected
results, but by the nature of it, and as you're warned about in the
docs you quoted yourself, it is not a good idea and probably won't work
how you expect it to. I don't know what else to say?
 
S

sln

(e-mail address removed) wrote:



It says it should not be used for rounding, for TWO reasons.
Thats two references to ROUNDING you say it doesen't use.

sln

Or... it's that it could be used for rounding, but that it's not a
good idea, as you could have unexpected results.
[snip]
And just what is that?

sln

It might appear to be working fine and give you the desired/expected
results, but by the nature of it, and as you're warned about in the
docs you quoted yourself, it is not a good idea and probably won't work
how you expect it to. I don't know what else to say?

It is certainty for you, you won't and can't depend on Perl intrinsic functions.
Because for you, they work intermittentely and with unexpected results.

I wish you luck.

sln
 
T

Tim Greer

(e-mail address removed) wrote:



It says it should not be used for rounding, for TWO reasons.
Thats two references to ROUNDING you say it doesen't use.

sln

Or... it's that it could be used for rounding, but that it's not a
good idea, as you could have unexpected results.
[snip]
And just what is that?

sln

It might appear to be working fine and give you the desired/expected
results, but by the nature of it, and as you're warned about in the
docs you quoted yourself, it is not a good idea and probably won't
work
how you expect it to. I don't know what else to say?

It is certainty for you, you won't and can't depend on Perl intrinsic
functions. Because for you, they work intermittentely and with
unexpected results.

I wish you luck.

sln

I didn't create it, I didn't write the documentation about it. While
I'm sure one could work with said function to do rounding, it's not
intended to round and suffers from side effects. For the record, in
many, many years of coding in Perl, I've never had a single program
have unexpected results. This is probably attributed to the fact that
I use the correct, intended functions for what they were designed for,
assuming I don't roll out my own. Probably, ignoring the intent of a
function not being what you think it is, and the docs warning not to
use it for the purpose you want, is why you're so confused? Don't take
that out on me. Obviously, you _can_ use int() to round, but I doubt
it's going to work like you think. You *do* know what truncation is,
right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top