In the following test case program I'M trying to figure out how I can use the remainder of divison to make a decision but I can not seem to get this test case to respond in the way that I think and want it to respond based upon the remainder of dividing two command line arguments (linux). It never out puts either one of my cout statements even when I'M feeding it 3 & 7 or 3 & 8.

Here's the program:

#include <iostream>
#include <stdlib.h>

int main( int argc, char* argv[] )
{
    //number1 = atoi(argv[1]);
    //number2 = atoi(argv[2]);

    if ( atoi( argv[1] ) % atoi( argv[2] ) == .33333 )
    {
        std::cout << "33333" << std::endl;
    }
    else if ( atoi( argv[1] ) % atoi( argv[2] ) == .66667 )
    {
        std::cout << "66667" << std::endl;
    }

    return 0;
}

I think you first need to store the remainder and then work with it.

int remainder = (atoi( argv[1] ) % atoi( argv[2]);

Now you work with remainder

if ( atoi( argv[1] ) % atoi( argv[2] ) == .33333 )

You misunderstood what the remainder is. The remainder is an integer value, not a fractional value. In other words, 7 % 3 gives 1 because 3 fits two times into 7 and then there is 1 left, that's the remainder.

Well, as mike and Suzie said, the remainder is the part that remains after dividing two numbers. It can take values from 0 to divisor-1 (the number by which it divides) in the usual integer division.
If you want to have the fractionar part of the number, and check if it's either 0.33333 or 0.666667 you should do it like this:

int main(int argc, char* argv[]){
    cout<<(atof(argv[1])/atof(argv[2]))-(atoi(argv[1])/atoi(argv[2]))<<endl;
    return 0;
}

where the first part computes the fractional division, meaning for 7/3 its 2.333333 and the 2nd part computes the integer division, which for 7/3 is 2 (discards the fractional part). So by this, 2.33333-2=0.333333, presumably what you wanted (given your poorly attempt)

== .33333
== .66667

Another way to do it by using the modf() function from cmath:

int main(int argc, char* argv[]){
    double intg;
    cout<<modf(atof(argv[1])/atof(argv[2]), &intg)<<endl;
    return 0;
}

Edited 3 Years Ago by Lucaci Andrew

Comparing floating point values for equality is also fraught with peril. You'd be better off converting them to strings and then doing the comparison. That way the not insignificant work of handling a precise representation of the value is deferred to the standard library. A "close enough" test is often acceptable as well:

#include <cmath>
#include <type_traits>

template <
    typename T, 
    typename = std::enable_if<std::is_floating_point<T>::value>::type>
inline bool approximate_eq(T x, T y, T percent_tolerance)
{
    return std::fabs(x - y) / x <= percent_tolerance;
}

#include <iostream>

void test_difference(double x, double y, double percent_tolerance)
{
    std::cout << x << " and " << y << " are "
              << (approximate_eq(x, y, percent_tolerance) ? "" : "NOT ")
              << "within " << percent_tolerance * 100 << "% of each other\n";
}

int main()
{
    test_difference(2.5, 2.3, .05);
    test_difference(2.5, 2.4, .05);
    test_difference(2.5, (1.0 * 2 + .5), .05);
    test_difference(2.5, 2.6, .05);
    test_difference(2.5, 2.7, .05);
}
This article has been dead for over six months. Start a new discussion instead.