I have the code
unsigned long long f(unsigned long long x, unsigned long long y){
return sqrt( ( (5*x*x + 1) * (5*y*y +1) - 1 ) / 4 );
}
but if x or y is too big, the thing overflows even though the output is supposed to be small. is there a way around this?
Split the argument of the square root up algebraically, maybe?:
Or split it up further based on your needs.
If
xis big enough you can ignore the+1and make the first term:and similarly with
yfor the second term, making itEDIT: After OP edited his question to be:
You can in fact split things up. You just have to be a little bit more careful. Bottom line is that if
xis really big, then the+1can be ignored. Ifyis really big then the-5can be ignored. If both(3*x*x+1)and(6*y*y-5)are positive and either is really big, then the-1can be ignored. You can use these tips and some additional surrounding logic to break this down a bit further. Like this:You can optimize this, but this is just meant to show the point.