How do I check if a string is a number (float)?

Which, not only is ugly and slow I’d dispute both. A regex or other string parsing method would be uglier and slower. I’m not sure that anything much could be faster than the above. It calls the function and returns. Try/Catch doesn’t introduce much overhead because the most common exception is caught without an extensive … Read more

Random float number generation

rand() can be used to generate pseudo-random numbers in C++. In combination with RAND_MAX and a little math, you can generate random numbers in any arbitrary interval you choose. This is sufficient for learning purposes and toy programs. If you need truly random numbers with normal distribution, you’ll need to employ a more advanced method. This will generate a number … Read more

Best way to generate a random float in C#

Best approach, no crazed values, distributed with respect to the representable intervals on the floating-point number line (removed “uniform” as with respect to a continuous number line it is decidedly non-uniform): (*) … check here for subnormal floats Warning: generates positive infinity as well! Choose exponent of 127 to be on the safe side. Another approach which will give … Read more

Floating Point Exception C++ Why and what is it?

A “floating point number” is how computers usually represent numbers that are not integers — basically, a number with a decimal point. In C++ you declare them with float instead of int. A floating point exception is an error that occurs when you try to do something impossible with a floating point number, such as divide by zero.

1e-9 or -1e9, which one is correct?

Neither is more correct than the other. They just represent different values. 1e-9 is 0.000000001; the minus sign applies to the exponent. -1e9 is -1000000000.0; the minus sign applies to the number itself. The e (or E) means “times 10-to-the”, so 1e9 is “one times ten to the ninth power”, and 1e-9 means “one times ten to the negative ninth power”. In mathematical scientific notation, this … Read more