You are here:

C++/c++ querry

Advertisement


Question
why the output of
400*400/400 is 72

Answer
Hi, Anuj.

The problem is that the calculation is being performed using shorts (2-byte numbers).  The maximum number that a 2-byte data type can contain is 65535.  So, consider:

   400 * 400 = 160000
   160000 % 65535 = 28930
   28930 / 400 = 72.325

The second line, where I do a modulus by 65535 yields what the wrap-around value for the calculation would end up being.  The end result, 72.325, is clipped to 72 because your data type is not a floating point type.

So, when using 2-byte number types, the maximum value of 65535 will cause large calculations to return bad results, such as your case where a calculation that should clearly equate to 400 is equating to 72.

Hope that clears everything up.  If you have further questions, please do not hesitate to ask.  I'm here to help. :)

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.