Jump to content
  • Advertisement
Sign in to follow this  

Division by zero (C++)

This topic is 4150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically, I have a and b (floats), I add them up and divide by zero - that's c (float). Now, I tell std::cout to print c and it prints 1.#INF Ok, here is my code:
#include <iostream>

int main()
	float a;
	float b;
	float c;
	std::cout << "type a number" << std::endl;
	std::cin >> a;
	std::cout << "Great! now type another one" << std::endl;
	std::cin >> b;
	c = (a (plus) b)/0; //the problem here is that the forum source tags won't display the 'plus sign', but there was suposed to be one here.
	std::cout << "c is: " << c << std::endl;
	std::cin >> b;
	return 0;

The strange part is that, if I replace a and b by 1 (but not both by 1.0, then it will work fine), then the compiler will get me an error in Visual C++ Express, and in Code::blocks, it will compile but it will crash. My processor is a Pentium 4, 3.0 Ghz. Does anybody know what's going on?

Share this post

Link to post
Share on other sites
floats aren't like ints (http://en.wikipedia.org/wiki/Floating_point)

Division by zero with floats does not crash (rather it gives that result you posted). Division by zero with ints does crash. By replacing the a and b with 1 you are making it integer division.

It's "fine" to divide by zero you just get that infinity value for the float. That's why with floats you generally want to check for zero before performing the division; otherwise you get bizarro values that ruin your simulations.


Share this post

Link to post
Share on other sites
The problems with the + signs not appearing is limited to the Show Preview link

+, ++, +++, ++++, and so on do not appear in Show Preview

However they do appear in the forum

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!