Integral cast

Post here if you don't find any other place for your post. But please, stay on-topic: algorithms, programming or something related to this web site and its services.

Moderator: Board moderators

Post Reply
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Integral cast

Post by Dominik Michniewski »

Could anyone tell me, why, in many cases, construction like this:

Code: Select all

(long int)sqrt(2.0)
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

Code: Select all

(long int)floor(sqrt(2.0))
all is OK. I don't worry about overflow, because I do it for small value. 2.0 is only example ....

Besta regards
Dominik
Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post 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
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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 :-))
wsarzynski
New poster
Posts: 19
Joined: Fri Oct 04, 2002 7:50 pm
Location: Warsaw, Poland

Post 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;
}                 
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post 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)))
Lord Henry
New poster
Posts: 1
Joined: Sun Oct 13, 2002 6:25 pm
Location: Brasil

Post by Lord Henry »

Sorry, made a mistake.

Best Regards
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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 ...
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post 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.
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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
Post Reply

Return to “Other words”