Difference between Math.Floor() and Math.Truncate()

Math.Floor rounds down, Math.Ceiling rounds up, and Math.Truncate rounds towards zero. Thus, Math.Truncate is like Math.Floor for positive numbers, and like Math.Ceiling for negative numbers. Here’s the reference. For completeness, Math.Round rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. Reference. See also: … Read more

What is the standard way to add N seconds to datetime.time in Python?

You can use full datetime variables with timedelta, and by providing a dummy date then using time to just get the time value. For example: import datetime a = datetime.datetime(100,1,1,11,34,59) b = a + datetime.timedelta(0,3) # days, seconds, then other fields. print(a.time()) print(b.time()) results in the two values, three seconds apart: 11:34:59 11:35:02 You could … Read more

JavaScript math, round to two decimal places [duplicate]

NOTE – See Edit 4 if 3 digit precision is important var discount = (price / listprice).toFixed(2); toFixed will round up or down for you depending on the values beyond 2 decimals. Example: http://jsfiddle.net/calder12/tv9HY/ Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed Edit – As mentioned by others this converts the result to a string. To avoid this: var discount = … Read more

What is the difference between ‘/’ and ‘//’ when used for division?

In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division. In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import … Read more

Understanding “randomness”

Just a clarification Although the previous answers are right whenever you try to spot the randomness of a pseudo-random variable or its multiplication, you should be aware that while Random() is usually uniformly distributed, Random() * Random() is not. Example This is a uniform random distribution sample simulated through a pseudo-random variable: BarChart[BinCounts[RandomReal[{0, 1}, 50000], … Read more

Designing function f(f(n)) == -n

You didn’t say what kind of language they expected… Here’s a static solution (Haskell). It’s basically messing with the 2 most significant bits: f :: Int -> Int f x | (testBit x 30 /= testBit x 31) = negate $ complementBit x 30 | otherwise = complementBit x 30 It’s much easier in a … Read more

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