division... strange behaviour i dont understand

C

clqrq

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

Ian Collins

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?
 
M

Murali Krishna

Ian said:
What's an unsigned float?

yes, unsigned float that is not compiling on my compiler.
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
 
Z

Zara

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
 
W

wanzi8552

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.
 
I

Ian Collins

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.
 
P

Pete Becker

Zara said:
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".
 
J

Jim Langston

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top