By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

division... strange behaviour i dont understand

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
<cl***@yahoo.dewrote in message
news:11*********************@b28g2000cwb.googlegro 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");

Jul 12 '06 #8

P: n/a
Jim Langston wrote:
<cl***@yahoo.dewrote in message
news:11*********************@b28g2000cwb.googlegro 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 12 '06 #9

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:e9**********@news.datemas.de...
Jim Langston wrote:
><cl***@yahoo.dewrote in message
news:11*********************@b28g2000cwb.googlegr oups.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.
Jul 12 '06 #10

P: n/a
Jim Langston wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:e9**********@news.datemas.de...
>Jim Langston wrote:
>><cl***@yahoo.dewrote in message
news:11*********************@b28g2000cwb.googleg roups.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.
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.