NO

Author Topic: remainder function  (Read 2307 times)

Griffin

  • Guest
remainder function
« on: October 12, 2018, 04:40:37 PM »
Allright, this is strange.

remainder( 0.11, 0.2 ) returns -0.09000. That can't be right, no?

Test case:
---------------8<------------------
#include <stdio.h>
#include <math.h>

int main( void )
{
    printf( "Remainder = %f\n", remainder( 0.11, 0.2 ) );
}
« Last Edit: October 12, 2018, 04:42:53 PM by Griffin »

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: remainder function
« Reply #1 on: October 12, 2018, 06:34:42 PM »
I tried GCC with the same result.
best regards
 Alex ;)

Offline jack

  • Member
  • *
  • Posts: 66
Re: remainder function
« Reply #2 on: October 12, 2018, 07:16:05 PM »
it may be that you have a misunderstanding of the function
from The GNU C Library documentation
Quote
Function: double remainder (double numerator, double denominator)
These functions are like fmod except that they round the internal quotient n to the nearest integer instead of towards zero to an integer. For example, remainder (6.5, 2.3) returns -0.4, which is 6.5 minus 6.9.

The absolute value of the result is less than or equal to half the absolute value of the denominator. The difference between fmod (numerator, denominator) and remainder (numerator, denominator) is always either denominator, minus denominator, or zero.

If denominator is zero, remainder signals a domain error.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: remainder function
« Reply #3 on: October 14, 2018, 05:57:33 PM »
The result seems correct. The help file doesn't say much, it only echoes what the C standard says. If I find myself having a really great day (unlikely), I may improve the text for this function...
/Pelle