Page 1 of 1
Integral cast
Posted: Mon Sep 23, 2002 7:54 am
by Dominik Michniewski
Could anyone tell me, why, in many cases, construction like this:
is the reason of error? I expect, that this construction trim a decimal part of double value ... (without rounding). I see too, that if I change this code to
all is OK. I don't worry about overflow, because I do it for small value. 2.0 is only example ....
Besta regards
Dominik
Posted: Mon Sep 23, 2002 8:40 am
by Picard
i think only negative non integer numbers cause a difference. so (int)sqrt(value) and (int)floor(sqrt(value)) shouldn't ever be different (in valid range).
integer casting only cuts the decimals: (int)-1.5=-1, but floor returns the largest integer that is less than or equal to the value: (int)floor(-1.5)=-2
Posted: Mon Sep 23, 2002 10:01 am
by Dominik Michniewski
OK, thanks.
But if I only want to know that [+/-]xxx.yyyy is xxx that I should get the same value ? I got WA a few times in i.e. 10288 if I use the first method ... (without floor() )....
But thanks very much Picard

)
Posted: Sat Oct 12, 2002 1:15 am
by wsarzynski
is right. I dont know standard c function to round towards zero.
In MSVC i use this routine, but i dont know
how to translate it to gcc inline asm.
Code: Select all
const unsigned short int Round_Zero= 0x0000; // towards zero
const unsigned short int Round_Nearest= 0x0C00; // nearest
const unsigned short int Round_Up= 0x0400; // towards+Inf
const unsigned short int Round_Down= 0x0800; // towards -Inf
inline int irint(double f, unsigned short int rm)
{
unsigned short int a1, a2;
int rv;
__asm {
fstcw [a2]
mov ax, [a2]
or ax, 0xE3F
xor ax, [rm]
mov [a1], ax
fldcw [a1]
fld [f]
fistp [rv]
fldcw [a2]
}
return rv;
}
Posted: Sat Oct 12, 2002 4:43 pm
by Stefan Pochmann
Dominik: I don't believe that those two give different values. Could we see the context, please? And do they still behave the same way when you put extra parentheses around the expressions? I.e. compare
((long int)sqrt(2.0))
and
((long int)floor(sqrt(2.0)))
Posted: Sun Oct 13, 2002 6:27 pm
by Lord Henry
Sorry, made a mistake.
Best Regards
Posted: Mon Oct 14, 2002 12:42 pm
by Dominik Michniewski
to Stefan Pochmann:
I don't have a lot of time in this moment to search in my solutions this problem, but If I only have it, I send to you code which contains this error ...
Posted: Mon Oct 14, 2002 12:57 pm
by Dominik Michniewski
to STefan Pochmann: (again)
As I correct remember context was:
in function
Code: Select all
long int table[X];
int i;
double d = 0.0;
for(i=0;i<X;i++)
{
d += something_with_math_calculation(some_parameters);
table[i] = (long int)sqrt(d); // or something like log(d), I don't remember, but I think that it's not important ...
}
and I got WA
when I change line table
= .... to
Code: Select all
table[i] = (long int)floor(sqrt(d));
I got Accepted (without any other change in my code). If I found whole program, I post it ....
Best regards
Dominik
Posted: Mon Oct 14, 2002 9:46 pm
by Stefan Pochmann
Ok, so at least it's not part of a larger expression, so it's not a parentheses-problem. But the difference between sqrt and log is very important! sqrt will never return something negative, so I doubt that floor-or-not-floor makes a difference. But log can easily return negative numbers and then you're in the situation described by Picard.
Posted: Tue Oct 15, 2002 7:56 am
by Dominik Michniewski
You have right Stefan and I know about it ....
But result of expression is in such range, that log must return value greater than zero .... If I correct remember, this function counting length of some numbers (postitive and greater than 1).
I was very confused, when I got WA and after change - Accepted. This was a reason, why I start this thread .....
Best regards
Dominik