2

From what I understand reading the documentation of Math::BigFloat, the following should be the code to round a number up, but it doesn't seem to work.

#!/usr/bin/perl

use strict;
use warnings;
use Math::BigFloat;

my $x = Math::BigFloat->new('2.3');
$x->ffround(0, '+inf');
print "$x\n"; # -> 2

What should I do in order to always round the number up and, e.g., in this example get the number 3 as output.

4

1 回答 1

5

舍入模式仅在从两个可能结果的中间进行舍入时才会影响行为:

#!/usr/bin/perl
use warnings;
use strict;

use Math::BigFloat;

my $n = Math::BigFloat->new('2.5');

print $n->copy->ffround(1, 'zero');           # 2
print $n->copy->ffround(1, '+inf');           # 3
print $n->copy->ffround(1, 'odd');            # 3
print $n->copy->ffround(1, 'even');           # 2

你想要的是bceil

my $m = Math::BigFloat->new('2.3');

print $m->copy->bceil();                      # 3
于 2013-05-27T16:08:48.720 回答