Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate the float reminder for the two float values

I have two float values, 'a' and 'b' .

I need to calculate the reminder of these two float values, and it must be a float value.

Let

float a = 1.1;
float b = 0.5;

So the remainder 'r' should be accurate value

i.e. r = a % b

r = 1.1 % 0.5

  0.5) 1.1 (2
       1.0
     ______

       0.1

  r = 0.1

But it causes to an error invalid operand for float values.

How to do it?

like image 536
Ranga Avatar asked Apr 05 '12 09:04

Ranga


People also ask

Can we obtain remainder in floating point?

Remainder cannot be obtain in floating point division. Explanation: fmod(x,y) - Calculates x modulo y, the remainder of x/y. This function is the same as the modulus operator.

How do you find the remainder of a float?

fmod() — Calculate Floating-Point RemainderThe fmod() function calculates the floating-point remainder of x/y. The absolute value of the result is always less than the absolute value of y. The result will have the same sign as x.

How do you find the modulus of a float in C++?

The remainder() function in C++ computes the floating point remainder of numerator/denominator (rounded to nearest). where rquote is the result of x/y , rounded towards the nearest integral value (with halfway cases rounded towards the even number).

How do you find the remainder of a floating point in C?

For this, we can use the remainder() function in C. The remainder() function is used to compute the floating point remainder of numerator/denominator. So the remainder(x, y) will be like below. The rquote is the value of x/y.


2 Answers

In C , C++ and Objective-C that would be fmod.

like image 87
Jon Avatar answered Nov 14 '22 00:11

Jon


use fmod()

#include <math.h>
double x,y,z;
x = 1.1;
y = 0.5;
z = fmod(x,y)

Don't tforget the -lm liker flag if you are on linux/unix/mac-osx/.

for more info

$man fmod
like image 39
Aftnix Avatar answered Nov 13 '22 22:11

Aftnix