Velocity Reviews > C++ > division... strange behaviour i dont understand

# division... strange behaviour i dont understand

clqrq@yahoo.de
Guest
Posts: n/a

 07-10-2006
can please someone explain why i get the message-box?
im damn shure 500 divided by 300 doesnt equal to zero ;-\

__________________________________
unsigned short uiNumbers = 500;
unsigned float ufFactor = 300.0;
ufFactor = ufFactor / uiNumbers;
if (ufFactor == 0) AfxMessageBox("0");

Ian Collins
Guest
Posts: n/a

 07-10-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> can please someone explain why i get the message-box?
> im damn shure 500 divided by 300 doesnt equal to zero ;-\
>
> __________________________________
> unsigned short uiNumbers = 500;
> unsigned float ufFactor = 300.0;

What's an unsigned float?

--
Ian Collins.

Murali Krishna
Guest
Posts: n/a

 07-10-2006

Ian Collins wrote:
> (E-Mail Removed) wrote:
> > can please someone explain why i get the message-box?
> > im damn shure 500 divided by 300 doesnt equal to zero ;-\
> >
> > __________________________________
> > unsigned short uiNumbers = 500;
> > unsigned float ufFactor = 300.0;

>
> What's an unsigned float?
>
> --
> Ian Collins.

yes, unsigned float that is not compiling on my compiler.

> > im damn shure 500 divided by 300 doesnt equal to zero ;-\

> ufFactor = ufFactor / uiNumbers;

any way, that's not 500 / 300.

it is 300 / 500. Should lead to 0.6 if ufFactor is float.
and 0 if it is integral.

-- Murali Krishna

Zara
Guest
Posts: n/a

 07-10-2006
On 9 Jul 2006 21:32:18 -0700, (E-Mail Removed) wrote:

>can please someone explain why i get the message-box?
>im damn shure 500 divided by 300 doesnt equal to zero ;-\
>
>__________________________________
>unsigned short uiNumbers = 500;
>unsigned float ufFactor = 300.0;
>ufFactor = ufFactor / uiNumbers;
>if (ufFactor == 0) AfxMessageBox("0");

unsigned float?

Anyway, it seems thta ufFactor (which should have the value 300.0/500)
is converted to int (thus, rounded to 0) before being compared.

Try :

if (ufFactor == 0.0) AfxMessageBox("0");

This is an tricky comparison, as comparison with doubles (and floats)
is subject to lots of problems due to rounding. You should take a look
at this list, siblig list comp.lang.c++.moderated and cousin lists
comp.lang.c and comp.lang.c.moderated about floating point comparisons

Regards,

Zara

wanzi8552@gmail.com
Guest
Posts: n/a

 07-10-2006
(E-Mail Removed) wrote:
> can please someone explain why i get the message-box?
> im damn shure 500 divided by 300 doesnt equal to zero ;-\
>
> __________________________________
> unsigned short uiNumbers = 500;
> unsigned float ufFactor = 300.0;
> ufFactor = ufFactor / uiNumbers;
> if (ufFactor == 0) AfxMessageBox("0");

Because unsigned float is not a defined type in C++, the ufFactor will
then be automatically converted to unsigned int.

P.S. Any decent compiler should be able to spot the problem and give
some warnings.

Ian Collins
Guest
Posts: n/a

 07-10-2006
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>
>>can please someone explain why i get the message-box?
>>im damn shure 500 divided by 300 doesnt equal to zero ;-\
>>
>>__________________________________
>>unsigned short uiNumbers = 500;
>>unsigned float ufFactor = 300.0;
>>ufFactor = ufFactor / uiNumbers;
>>if (ufFactor == 0) AfxMessageBox("0");

>
>
> Because unsigned float is not a defined type in C++, the ufFactor will
> then be automatically converted to unsigned int.
>

No, it's just plain wrong and should fail to compile.

--
Ian Collins.

Pete Becker
Guest
Posts: n/a

 07-10-2006
Zara wrote:
>
> unsigned float?
>
> Anyway, it seems thta ufFactor (which should have the value 300.0/500)
> is converted to int (thus, rounded to 0) before being compared.
>

If ufFactor is really some floating point-type, then 0 would be
converted to a floating-point type, not the other way around.

> Try :
>
> if (ufFactor == 0.0) AfxMessageBox("0");
>
> This is an tricky comparison, as comparison with doubles (and floats)
> is subject to lots of problems due to rounding.

There's nothing tricky involved in checking whether a value is 0, and
there are no floating-poitn roundoff issues in this code. Most likely,
"unsigned float" was actually "unsigned int".

Jim Langston
Guest
Posts: n/a

 07-12-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> can please someone explain why i get the message-box?
> im damn shure 500 divided by 300 doesnt equal to zero ;-\

No, but 300 divided by 500 is 0 in integer math.

> _________________________________
> unsigned short uiNumbers = 500;
> unsigned float ufFactor = 300.0;
> ufFactor = ufFactor / uiNumbers;
> if (ufFactor == 0) AfxMessageBox("0");

Victor Bazarov
Guest
Posts: n/a

 07-12-2006
Jim Langston wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> can please someone explain why i get the message-box?
>> im damn shure 500 divided by 300 doesnt equal to zero ;-\

>
> No, but 300 divided by 500 is 0 in integer math.
>
>> _________________________________
>> unsigned short uiNumbers = 500;
>> unsigned float ufFactor = 300.0;

WTF is "unsigned float"?

>> ufFactor = ufFactor / uiNumbers;
>> if (ufFactor == 0) AfxMessageBox("0");

V
--

Jim Langston
Guest
Posts: n/a

 07-12-2006

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:e93a00\$apu\$(E-Mail Removed)...
> Jim Langston wrote:
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ups.com...
>>> can please someone explain why i get the message-box?
>>> im damn shure 500 divided by 300 doesnt equal to zero ;-\

>>
>> No, but 300 divided by 500 is 0 in integer math.
>>
>>> _________________________________
>>> unsigned short uiNumbers = 500;
>>> unsigned float ufFactor = 300.0;

>
> WTF is "unsigned float"?

A syntax error in his code which has already been discussed.