DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Parsing error (http://www.daniweb.com/forums/thread158919.html)

Fouly Nov 22nd, 2008 7:15 am
Parsing error
 
Hi all,
I used a function called GetRandomVariable to return a double data type and here's its code:

double GetRandomNumber(void)
{
    // Zi = (AZi-1 + C)(mod m)
    // Zi = (5Zi-1 + 3)(mod 16)
    RandomVariableGenerator::Znode = (double)((5.0*RandomVariableGenerator::Znode + 3) % 16);
    return RandomVariableGenerator::Znode/16;
}
 static double Znode = 7.0;
but unfortunately i got the following error:

Error 1 error C2296: '%' : illegal, left operand has type 'double'

William Hemsworth Nov 22nd, 2008 8:16 am
Re: Parsing error
 
double doesn't support the Modulus operator, you will have to implement it yourself if you want to do this.

Alex Edwards Nov 22nd, 2008 8:21 am
Re: Parsing error
 
There is hope!

Use fmod to resolve the modulus between two doubles


#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main(){

    double x = fmod(4.5, 3.22);
    cout << x << endl; // 1 R 1.28, so 1.28 should print out
    cin.get();
    return 0;
}

Fouly Nov 22nd, 2008 9:22 am
Re: Parsing error
 
Yes it works...i used the fmod function instead of implementing it by my self. thanks a lot Alex & William


All times are GMT -4. The time now is 2:21 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC