Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you calculate div and mod of floating point numbers?

In Perl, the % operator seems to assume integers. For instance:

sub foo {
    my $n1 = shift;
    my $n2 = shift;
    print "perl's mod=" . $n1 % $n2, "\n";
    my $res = $n1 / $n2;
    my $t = int($res);
    print "my div=$t", "\n";
    $res = $res - $t;
    $res = $res * $n2;
    print "my mod=" . $res . "\n\n";
}   

foo( 3044.952963, 7.1 );
foo( 3044.952963, -7.1 );
foo( -3044.952963, 7.1 );
foo( -3044.952963, -7.1 );

gives

perl's mod=6
my div=428
my mod=6.15296300000033

perl's mod=-1
my div=-428
my mod=6.15296300000033

perl's mod=1
my div=-428
my mod=-6.15296300000033

perl's mod=-6
my div=428
my mod=-6.15296300000033

Now as you can see, I've come up with a "solution" already for calculating div and mod. However, what I don't understand is what effect the sign of each argument should have on the result. Wouldn't the div always be positive, being the number of times n2 fits into n1? How's the arithmetic supposed to work in this situation?

like image 554
bugmagnet Avatar asked Jan 30 '09 06:01

bugmagnet


People also ask

How do you find the modulo of a float?

The modulus is basically finding the remainder. For this, we can use the remainder() function in C. The remainder() function is used to compute the floating point remainder of numerator/denominator.

Does the modulus operator work with floating point numbers?

Answer. Yes, the Python modulo operator will work with floating point numbers. This will take precedence, so will be either zero or a float.

What is div and mod?

Modulus: The remainder that is left over when a number is divided by another. Some programming languages will use the % symbol for MOD. 16 MOD 3 = 1 (16 / 3 = 5, with 1 left over) DIV. Integer division: Used to find the quotient (integer number before the decimal point) after division.

Can we use modulo division operator with float and int?

15) Can you use C Modulo Division operator % with float and int? Explanation: Modulo Division operator % in C language can be used only with integer variables or constants.


1 Answers

The title asks one question, the body another. To answer the title question, just as in C, the % operator is an integer modulus, but there's a library routine "fmod" that's a floating point modulus.

use POSIX "fmod";

sub foo {
    my $n1 = shift;
    my $n2 = shift;
    print "perl's fmod=" . fmod($n1,$n2), "\n";
    my $res = $n1 / $n2;
    my $t = int($res);
    print "my div=$t", "\n";
    $res = $res - $t;
    $res = $res * $n2;
    print "my mod=" . $res . "\n\n";
}

foo( 3044.952963, 7.1 );
foo( 3044.952963, -7.1 );
foo( -3044.952963, 7.1 );
foo( -3044.952963, -7.1 );

gives

perl's fmod=6.15296300000033
my div=428
my mod=6.15296300000033

perl's fmod=6.15296300000033
my div=-428
my mod=6.15296300000033

perl's fmod=-6.15296300000033
my div=-428
my mod=-6.15296300000033

perl's fmod=-6.15296300000033
my div=428
my mod=-6.15296300000033
like image 168
ysth Avatar answered Nov 07 '22 06:11

ysth