0

I was trying to validate if the first floating number could be fully divided by the second floating number and I got something like these:

$first = 10.20; // or 5.10, 7.14, 9.18
$second = 1.02;

var_dump(fmod($first, $second));

// output is showing
float(1.02)

But if I try to divide below these numbers by 1.02 fmod()works nicely.I don't understand whyfmod() behaving like this? Any answer to this question will help me to understand the fact.

$first = 4.08; // or 2.04, 3.06, 6.12, 8.16
$second = 1.02;

var_dump(fmod($first, $second));

// output is showing
float(0)
1

1 Answer 1

1

It is because the fmod() function only returns the floating point modulos of the division made.

As in your first case the remainder is a floating point value it is returned by the function.

But in the below examples if we make divisions as

$first = 4.08; // or 2.04, 3.06, 6.12, 8.16
$second = 1.02;

We get the remainder as of type integer which is 0.041616.The problem here is the values shown by the function are limited only till the first decimal(in this case it is 0.0).But if we had to find the modulos of two numbers as

<?php
$x = 5.7;
$y = 1.3;
$r = fmod($x, $y);
?>

We will get the values as float(0.5).

Because the here the first digit after decimal point is not 0.

And hence in your case it is shown float(0) by the function.

For more details you can visit Click Here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.