How to round a number to significant figures in Python

You can use negative numbers to round integers: >>> round(1234, -3) 1000.0 Thus if you need only most significant digit: >>> from math import log10, floor >>> def round_to_1(x): … return round(x, -int(floor(log10(abs(x))))) … >>> round_to_1(0.0232) 0.02 >>> round_to_1(1234243) 1000000.0 >>> round_to_1(13) 10.0 >>> round_to_1(4) 4.0 >>> round_to_1(19) 20.0 You’ll probably have to take care … Read more

What does gcc’s ffast-math actually do?

-ffast-math does a lot more than just break strict IEEE compliance. First of all, of course, it does break strict IEEE compliance, allowing e.g. the reordering of instructions to something which is mathematically the same (ideally) but not exactly the same in floating point. Second, it disables setting errno after single-instruction math functions, which means … Read more

What are the mathematical/computational principles behind this game?

Finite Projective Geometries The axioms of projective (plane) geometry are slightly different than the Euclidean geometry: Every two points have exactly one line that passes through them (this is the same). Every two lines meet in exactly one point (this is a bit different from Euclid). Now, add “finite” into the soup and you have … Read more

numpy max vs amax vs maximum

np.max is just an alias for np.amax. This function only works on a single input array and finds the value of maximum element in that entire array (returning a scalar). Alternatively, it takes an axis argument and will find the maximum value along an axis of the input array (returning a new array). >>> a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)