JavaScript % (modulo) gives a negative result for negative numbers
Number.prototype.mod = function (n) { “use strict”; return ((this % n) + n) % n; }; Taken from this article: The JavaScript Modulo Bug
Number.prototype.mod = function (n) { “use strict”; return ((this % n) + n) % n; }; Taken from this article: The JavaScript Modulo Bug
You want to cast the numbers: double num3 = (double)num1/(double)num2; Note: If any of the arguments in C# is a double, a double divide is used which results in a double. So, the following would work too: double num3 = (double)num1/num2; For more information see: Dot Net Perls
I assume entropy was mentioned in the context of building decision trees. To illustrate, imagine the task of learning to classify first-names into male/female groups. That is given a list of names each labeled with either m or f, we want to learn a model that fits the data and can be used to predict … Read more
NewValue = (((OldValue – OldMin) * (NewMax – NewMin)) / (OldMax – OldMin)) + NewMin Or a little more readable: OldRange = (OldMax – OldMin) NewRange = (NewMax – NewMin) NewValue = (((OldValue – OldMin) * NewRange) / OldRange) + NewMin Or if you want to protect for the case where the old range is … Read more
With JDK1.6, you can use the built-in Javascript engine. import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; public class Test { public static void main(String[] args) throws ScriptException { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName(“JavaScript”); String foo = “40+2”; System.out.println(engine.eval(foo)); } }
Found an elegant solution: int pageCount = (records + recordsPerPage – 1) / recordsPerPage; Source: Number Conversion, Roland Backhouse, 2001
Warning: timeit results may vary due to differences in hardware or version of Python. Below is a script which compares a number of implementations: ambi_sieve_plain, rwh_primes, rwh_primes1, rwh_primes2, sieveOfAtkin, sieveOfEratosthenes, sundaram3, sieve_wheel_30, ambi_sieve (requires numpy) primesfrom3to (requires numpy) primesfrom2to (requires numpy) Many thanks to stephan for bringing sieve_wheel_30 to my attention. Credit goes to Robert … Read more
There’s an old trick to do this with only one comparison/branch. Whether it’ll really improve speed may be open to question, and even if it does, it’s probably too little to notice or care about, but when you’re only starting with two comparisons, the chances of a huge improvement are pretty remote. The code looks … Read more
Calculate the distance between two coordinates by latitude and longitude, including a Javascript implementation. West and South locations are negative. Remember minutes and seconds are out of 60 so S31 30′ is -31.50 degrees. Don’t forget to convert degrees to radians. Many languages have this function. Or its a simple calculation: radians = degrees * … Read more
The type-safe C++ version: template <typename T> int sgn(T val) { return (T(0) < val) – (val < T(0)); } Benefits: Actually implements signum (-1, 0, or 1). Implementations here using copysign only return -1 or 1, which is not signum. Also, some implementations here are returning a float (or T) rather than an int, … Read more