5

Why is the intpart output of modf forced to be a double? Isn't the output, by definition, going to always be an int, unless the integer part of the input is itself a double.

  int main ()
{
  double p, fractpart, intpart;

  fractpart = modf (p , &intpart);

} 
8
  • What type would you prefer, given that the range can frequently be larger than any integer type you can depend upon having available? C doesn't have dynamic typing (or anything similar) to allow it to decide the return type based on the value. Aug 10, 2014 at 2:26
  • 1
    Isn't double inherently less precise than int. So couldn't I output an int which is more precise and should be perfectly suited to handle the output.
    – Mo1
    Aug 10, 2014 at 2:27
  • No, double is not inherently less precise than int. In a typical case, double can represent every integer in the range -2e53..2e53 precisely. Aug 10, 2014 at 2:29
  • 1
    Basically, back when it was defined, many many years ago, there weren't integer types big enough to hold the result. Aug 10, 2014 at 2:32
  • 1
    assigning a double a value like 10.0 will fail when compared with 10, because floating points and double's can't always exactly represent an integer value (from what I get it has something to do with IEEE). That is what I mean by less precise. So if this is the case then isn't an INT output better if you need that absoluteness.
    – Mo1
    Aug 10, 2014 at 3:10

1 Answer 1

2

The modf function breaks a double argument into integral and fractional parts; for example, given 3.75 it returns 0.75 and stores 3.0 in the object pointed to by its second argument.

The question is, what should happen if you call it with a value that's too big to fit in any integer type?

If it returned an int result, or even a long long or intmax_t result, it would have to deal with overflow somehow, which would likely require adding an extra parameter to distinguish valid results from overflows.

By returning a double result, overflow is not possible; for very large arguments, it can just return the argument value and set the fractional part to 0.0. It simplifies the function considerably. (If you want to convert the result to an integer you can do so -- but you should check the result against the bounds of the integer type you're using.)

On modern systems double is typically 64 bits, and can represent integers up to about 253 exactly. If you call modf with a value greater than 253, then the double value itself can't necessarily hold an exact integer value; having modf return even a 64-bit integer wouldn't provide any extra precision.

A long double, depending on the implementation, might be able to hold a wider range of exact integer values than even the widest integer type; on such a system, making modfl return an integer would lose precision relative to having it return long double.

So having modf (and modff and modfl) return an integer rather than a floating-point value would lose range without any corresponding gain in precision.

8
  • Floating point numbers in C use IEEE 754 encoding. This type of encoding uses a sign, a significand, and an exponent. Because of this encoding, you can never guarantee that you will not have a change in your value. stackoverflow.com/questions/5098558/float-vs-double-precision
    – Mo1
    Aug 10, 2014 at 3:23
  • Doesn't the IEE 754 encoding mean that a double will be less precise than an int. Are int's subject to the same variability?
    – Mo1
    Aug 10, 2014 at 3:25
  • I guess never mind...this comment seems to address my question. Under IEEE 754, it's easily guaranteed that there is no change in the values 0.5, 0.046875, or 0.376739501953125 versus their decimal representations. (These are all diadic rationals with numerator fitting in the mantissa and base-2 logarithm of the denominator fitting in the exponent.) stackoverflow.com/questions/5098558/float-vs-double-precision
    – Mo1
    Aug 10, 2014 at 3:31
  • @Mo1: C doesn't require IEEE 754. The width of int is implementation-defined, and can be as narrow as 16 bits. Aug 10, 2014 at 4:30
  • 1
    @Mo1: Floating-point numbers can and often do lose precision; for example a binary floating-point type can't represent 0.1 exactly. But it can represent 0.0 exactly, as well as any number within its range and precision that's equal to a whole number multiplied by a power of 2.0. float x = 0.0; will set x to exactly 0.0. A double is similar to float, but with greater range and precision. See David Goldberg's classic article "What Every Computer Scientist Should Know About Floating-Point Arithmetic". Aug 10, 2014 at 7:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.