Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide by zero without error

i need to obtain float NaN and infinity, but i can't use such constructions

0. / 0.
1. / 0.

because it cause compile time

error C2124: divide or mod by zero

EDIT, it is cool to have answers for where i can get this numbers (+1 for every), but is it possible to divide by zero?

like image 832
Yola Avatar asked Jun 08 '13 16:06

Yola


2 Answers

You can simply return a NaN or an infinity, for example:

return std::numeric_limits<float>::quiet_NaN();

or

return std::numeric_limits<float>::infinity();

See std::numeric_limits, from header <limits>.

like image 178
juanchopanza Avatar answered Oct 05 '22 21:10

juanchopanza


Use std::numeric_limits::infinity() and std::numeric_limits::quiet_NaN() or std::numeric_limits::signaling_NaN() from <limits>.

like image 34
Mankarse Avatar answered Oct 05 '22 22:10

Mankarse