How do I convert an integer to a float in JavaScript?

What you have is already a floating point number, they’re all 64-bit floating point numbers in JavaScript. To get decimal places when rendering it (as a string, for output), use .toFixed(), like this: function intToFloat(num, decPlaces) { return num.toFixed(decPlaces); } You can test it out here (though I’d rename the function, given it’s not an … Read more

Python numpy float16 datatype operations, and float8?

To the first question: there’s no hardware support for float16 on a typical processor (at least outside the GPU). NumPy does exactly what you suggest: convert the float16 operands to float32, perform the scalar operation on the float32 values, then round the float32 result back to float16. It can be proved that the results are … Read more

python difference between round and int

Note: The python implementation changed between 2.7 and 3.x. I corrected the answer accordingly. For the sake of completeness, let me add two more functions to your question and explain the differences between float(int(x)), math.floor(x), round(x) and math.ceil(x). Let’s start with a question: “What integer represents best the number 1.6?” We have two possible answers … Read more

Using floats with sprintf() in embedded C

Since you’re on an embedded platform, it’s quite possible that you don’t have the full range of capabilities from the printf()-style functions. Assuming you have floats at all (still not necessarily a given for embedded stuff), you can emulate it with something like: char str[100]; float adc_read = 678.0123; char *tmpSign = (adc_read < 0) … Read more

Why is converting between string and float wrong?

You should use “R” format string: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx. https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#RFormatString “R” or “r” Round-trip Result: A string that can round-trip to an identical number. Supported by: Single, Double, and BigInteger. Precision specifier: Ignored. float maxFloat = float.MaxValue; string s = maxFloat.ToString(“R”); // <- “R” float result = float.Parse(s); bool mustEqual = (maxFloat == result);

Why is FLT_MIN equal to zero?

It’s not actually zero, but it might look like zero if you inspect it using printf or NSLog by using %f. According to float.h (at least in Mac OS X 10.6.2), FLT_MIN is described as: /* Minimum normalized positive floating-point number, b**(emin – 1). */ Note the positive in that sentence: FLT_MIN refers to the … Read more

Why does integer division code give the wrong answer? [duplicate]

You’re dividing integers, which means that you’re using integer division. In integer division the fractional part of the result is thrown away. Try the following: float res = (float) quantity / standard; ^^^^^^^ The above forces the numerator to be treated as a float which in turn promotes the denominator to float as well, and … Read more

Can a Fibonacci function be written to execute in O(1) time?

Here is a near O(1) solution for a Fibonacci sequence term. Admittedly, O(log n) depending on the system Math.pow() implementation, but it is Fibonacci w/o a visible loop, if your interviewer is looking for that. The ceil() was due to rounding precision on larger values returning .9 repeating. Example in JS: function fib (n) { … Read more

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