Finding if one floating point number is a multiple of another in .NET

You would determine if the remainder is below an acceptable tolerance to your problem or if the remainder is very close to your multipleOf:

if (Math.Abs(remainder) < tolerance)
{
   //remainder is negligible so we'll say it's a multiple
}
else if (Math.Abs(multipleOf) - Math.Abs(remainder) < tolerance)
{
   //remainder is almost multiple of divisor so we'll say it's a multiple
}

Only you can decide on a small enough value for tolerance. Sometimes machine epsilon is used for this type of check but it might be too low. If v is very large and multipleOf very small we might say the problem is ill conditioned because the tolerance might need to be so high the results would be useless for the level of accuracy you require in your application. So searching for Conditioning and Precision might be of further interest as well.


Comments

  1. Foster

    • 2016/4/15

    You would determine if the remainder is below an acceptable tolerance to your problem or if the remainder is very close to your multipleOf : if (Math.

  2. Dakari

    • 2020/4/3

    1 Answer1. Active Oldest Votes. 4. You would determine if the remainder is below an acceptable tolerance to your problem or if the remainder is very close to your multipleOf: if (Math.Abs (remainder) < tolerance) { //remainder is negligible so we'll say it's a multiple } else if (Math.Abs (multipleOf) - Math.Abs (remainder) < tolerance) { //remainder is almost multiple of divisor so we'll say it's a multiple }

  3. Henrik

    • 2015/11/20

    You are not allowed to use % and / operators. Method 1 (Repeatedly subtract 5 from n) Run a loop and subtract 5 from n in the loop while n is 

  4. Rinaldi

    • 2019/10/27

    One possible way to solve this is to check both scenarios: modt = t % dt(abs(modt) <= tolerance) or (abs(dt - modt) <= tolerance) where I would expect tolerance = machine epsilon * abs(dt) / 2,based on this answer,but the modulo operation somehow introduces more error than that. It looks like:

  5. Kelmendi

    • 2015/6/3

    Compares this instance to a specified double-precision floating-point number and returns an integer that indicates whether the value of this instance is 

  6. Rayan

    • 2019/2/26

    There's no built-in way to see the exact decimal value of a floating point number in .NET, although you can do it with a bit of work. (See the bottom of this article for some code to do this.) By default, .NET formats a double to 15 decimal places, and a float to 7.

  7. Maddox

    • 2018/3/21

    We still need to explain the use of the other reserved biased exponent value of 255. This exponent is used to represent infinities and Not-a-Number values. If 

  8. Ibrahim

    • 2021/2/13

    The decimal type is just another form of floating point number - but unlike float and double, the base used is 10. If you haven't read the article linked above, now would be a good time to read it - I won't go into the basics of floating point numbers in this article.

  9. Cannon

    • 2019/12/3

    If the string is numeric or leading numeric then it will resolve to the corresponding float value, otherwise it is converted to zero ( 0 ). From other types ¶.

  10. Jerry

    • 2018/6/26

    To multiply two fixed point numbers, multiply the two numbers then shift right the defined number of fractional bits. To divide two fixed point numbers, shift the dividend left the defined number of fractional bits, then divide by the divisor. Chapter four of this paper has additional guidance on implementing binary fixed point numbers.

  11. Caspian

    • 2018/7/12

    Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in you should consider using one of Java's float-handling Numbers such as 

  12. Bennet

    • 2021/7/24

    Another way to measure the difference between a floating-point number and the One application of exact rounding occurs in multiple precision arithmetic.

  13. Morales

    • 2015/3/20

    Learn about precision vs accuracy and when to use int, double, Check it out! Single (aka float): A 32-bit floating point number.

  14. Jamir

    • 2021/4/13

    Finally , you might want to convert one type to another directly , in a type If , for example , you were to convert a very big floating - point number 

  15. Tristen

    • 2018/7/1

    Mixing values of the integer and floating - point types in an expression can This is the case if you turn off the compiler property Option Strict .

Comments are closed.

Recent Posts