0

Thanks in advance for everyone's help.

I'm trying to write a program in c++ that includes a function that calculates all possible combinations of f1, f2, f3; where 15 = f_total = ((f1) / (f1+f2+F3)). Whenever true, the program will output the values of f(n) to stdout. Where the f(n)'s are 3 randomly generated fractional doubles, that are produced by a locally defined function `double functRand_f(f_min, f_max).

So with the backdrop out of the way, I'm hoping someone can just assist me with the actual syntax of the equation itself. How do I structure the equation syntax within the context of my functions:

.....
double funcRand_f(double fMin, double fMax)
{ 
    double fRange = (fMax - fMin);
    double div = RAND_MAX /fRange; 
    return (fMax - fMin) * ((double)rand() / (double)RAND_MAX) +fMin;
} 

bool funcTotal_f(double x, double y, double z, double sum)
{
    return (((x) / (x+y+z)) == f_sum);
    return (f_total = sum); 
}

.... 

I've tried looping to see when a match takes place. Moved around the decimal point, but after several several hours of trying to get it to work properly, I just can't get it to work.

I have the f1, f2, f3 all being assigned random numbers in a for loop before being passed to the funcTotal_f, and checked to make sure the values are peristant in and out of the methods before being overloaded with a new value.

Any guidance or a point to a resource that could help me figure it out is much appreciated. Thanks.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 3
    `(((x) / (x+y+z)) == f_sum);` Never compare floating point calculations for equality. Floating point is not exact. Binary computing machines, i.e. your computer, do not work the same way as your algebra book. You also have two consecutive `return` statements. How is that supposed to work? – PaulMcKenzie Oct 05 '15 at 20:44
  • Have a read: https://isocpp.org/wiki/faq/newbie#floating-pt-errs – PaulMcKenzie Oct 05 '15 at 20:54
  • the second return "(f_total = sum); assigns the value to the variable which is added to a vector in the main part of the program. It may not be synatatically correct, but it is to keep track of matches. It really isn't there for anything in particular aside from that I wanted to see the values and see if there was any usable information for possible future expansion thereof. – Chris_Frost Oct 05 '15 at 20:59
  • I think I get what you're saying about floating points, so is just not possible for the statement to ever be true if I use anything other than an integer? – Chris_Frost Oct 05 '15 at 21:01
  • 1
    *the second return "(f_total = sum); assigns the value to the variable which is added to a vector in the main part of the program.* -- The second return does nothing. The compiler ignores it. – PaulMcKenzie Oct 05 '15 at 21:02
  • With floating point, you have to have a delta that is acceptable, and use that as your "equal". For example if the absolute value of the difference between the two quantities is < 0.000001 or something like that. – PaulMcKenzie Oct 05 '15 at 21:04
  • Paul, thanks for the wiki url, I read it and I'm going to try implementing one of the suggestions there and I'll update once I get it working. – Chris_Frost Oct 05 '15 at 21:08

0 Answers0