floating-point-conversion

Convert double to string with fixed point notation, no trailing zeroes and witout sprintf

不想你离开。 提交于 2019-12-08 07:08:30
This question has been asked a couple of times, but all the answers either refer to sprintf or involve deleting the trailing zeroes manually. Is there really no better way? is it not possible to achieve this with std::stringstream ? First you calculate how many potential digits you have before and after the decimal: int digits_before = 1 + (int)floor(log10(fabs(value))); int digits_after = std::numeric_limits<double>::digits10 - digits_before; Then you find out how many of those digits are zeros: double whole = floor(pow(10, digits_after) * fabs(value) + 0.5); while (digits_after > 0 && (whole

Writing an image with floating point values

三世轮回 提交于 2019-12-07 02:56:21
问题 My code reads an RGB image, processing it to produce floating-point results. How can I write these values as an image, maintaining these floating-point values, in Matlab 7.6.0 (R2008a)? 回答1: It is actually possible to fit 64 bits of pixel data (i.e. the number of bits needed to represent a double-precision floating-point value) into certain image formats, specifically a PNG. And you can even recover the data exactly. The key is to encode everything as uint16 values and use a Truecolor RGB

Understanding float variable comparison in if() [duplicate]

与世无争的帅哥 提交于 2019-12-06 16:26:18
问题 This question already has answers here : strange output in comparison of float with float literal (8 answers) Closed 5 years ago . Unable to find the reason for the following piece of code: #include <stdio.h> int main() { float f = 0.1; if (f == 0.1) printf("True"); else printf("False"); return 0; } The output is false. #include <stdio.h> int main() { float f = 0.1; if (f == (float)0.1) printf("True"); else printf("False"); return 0; } Now shows the correct output. Whats the reason behind

Extract mantissa, exponent and sign data from IEEE-754 double in VBA

混江龙づ霸主 提交于 2019-12-06 14:03:33
问题 How to extract mantissa, exponent and sign data from an IEEE-754 64-bit (double) floating-point number in VBA? Thanks Edit (after John Coleman comments). Before posting the original question, I have looked around for a solution and could only find how to do it in C (e.g. using structures with bit fields). Couldn't find anything for VBA. I have tried using VBA's bit operators (i.e. AND, OR, NOT, XOR) but this does not seem to give the expected result. For example, 1 expressed in single

Convert float to unsigned long to access float internals, in c #define

限于喜欢 提交于 2019-12-06 03:29:07
问题 I want to convert a float to a unsigned long , while keeping the binary representation of the float (so I do not want to cast 5.0 to 5 !). This is easy to do in the following way: float f = 2.0; unsigned long x = *((unsigned long*)&f) However, now I need to do the same thing in a #define , because I want to use this later on in some array initialization (so an [inline] function is not an option). This does not compile: #define f2u(f) *((unsigned long*)&f) If I call it like this: unsigned long

Bitwise Float To Int

…衆ロ難τιáo~ 提交于 2019-12-06 01:45:01
I am trying to figure out the algorithm to this but all I get with google is doing it with casting. I need to know the details. So if we have a float x and want to return its binary representation what do we need to do? I know we need to return the float if its NaN or a infinity but otherwise what are the steps? EDIT The function takes in an unsigned int, to be used as if it was a float, and then return the integer the number represents. I cannot use casting, just conditionals and bit-wise operators. Alternatively, use a union: typedef union { float f_; int i_; } FloatBits; FloatBits fb; fb.f_

Writing an image with floating point values

微笑、不失礼 提交于 2019-12-05 08:06:14
My code reads an RGB image, processing it to produce floating-point results. How can I write these values as an image, maintaining these floating-point values, in Matlab 7.6.0 (R2008a)? It is actually possible to fit 64 bits of pixel data (i.e. the number of bits needed to represent a double-precision floating-point value ) into certain image formats, specifically a PNG . And you can even recover the data exactly. The key is to encode everything as uint16 values and use a Truecolor RGB image (3 color planes of 16 bit data) along with an alpha transparency map (another 16 bits). Here's the

Convert scientific notation to decimal - python

帅比萌擦擦* 提交于 2019-12-05 06:29:19
How do I convert a scientific notation to floating point number? Here is an example of what I want to avoid: Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a=[78.40816326530613, 245068094.16326532] >>> print a[0]/a[1] 3.19944395589e-07 >>> print float(a[0]/a[1]) 3.19944395589e-07 >>> print float(a[0])/float(a[1]) 3.19944395589e-07 Dhara The scientific notation is just a convenient way of printing a floating point number. When there are a lot of leading zeros as in your example, the scientific notation

Why is IEEE-754 Floating Point not exchangable between platforms?

空扰寡人 提交于 2019-12-04 22:21:01
问题 It has been asserted that (even accounting for byte endian-ness) IEEE754 floating point is not guaranteed to be exchangeable between platforms. So: Why, theoretically, is IEEE floating point not exchangeable between platforms? Are any of these concerns valid for modern hardware platforms (e.g. i686, x64, arm)? If the concerns are valid, can you please demonstrate an example where this is the case (C or C++ is preferred)? Motivation: Several GPS manufacturers exchange their binary formats for

Understanding float variable comparison in if() [duplicate]

做~自己de王妃 提交于 2019-12-04 22:08:47
This question already has answers here : strange output in comparison of float with float literal (8 answers) Closed 5 years ago . Unable to find the reason for the following piece of code: #include <stdio.h> int main() { float f = 0.1; if (f == 0.1) printf("True"); else printf("False"); return 0; } The output is false. #include <stdio.h> int main() { float f = 0.1; if (f == (float)0.1) printf("True"); else printf("False"); return 0; } Now shows the correct output. Whats the reason behind this? Also what is the reason of this behavior. #include <stdio.h> main() { int n = 0, m = 0; if (n > 0)