468,456 Members | 1,642 Online

# division... strange behaviour i dont understand

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");

Jul 10 '06 #1
10 1566
cl***@yahoo.de 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.
Jul 10 '06 #2

Ian Collins wrote:
cl***@yahoo.de 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

Jul 10 '06 #3
On 9 Jul 2006 21:32:18 -0700, cl***@yahoo.de 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
Jul 10 '06 #4
cl***@yahoo.de 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.

Jul 10 '06 #5
wa*******@gmail.com wrote:
cl***@yahoo.de 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.
Jul 10 '06 #6
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".
Jul 10 '06 #7
<cl***@yahoo.dewrote in message
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");

Jul 12 '06 #8
Jim Langston wrote:
<cl***@yahoo.dewrote in message
>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
--
Jul 12 '06 #9

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:e9**********@news.datemas.de...
Jim Langston wrote:
><cl***@yahoo.dewrote in message
>>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.
Jul 12 '06 #10
Jim Langston wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:e9**********@news.datemas.de...
>Jim Langston wrote:
>><cl***@yahoo.dewrote in message
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.
Ah... Thanks. Sorry to butt in at the middle of the thread.
Jul 12 '06 #11

### This discussion thread is closed

Replies have been disabled for this discussion.