floating-point-conversion

Printing a float with commas in C [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-04 19:56:04
This question already has an answer here : Closed 6 years ago . Possible Duplicate: Output 1000000 as 1,000,000 and so on I have a float variable in the format xxxxxxxx.xx (Eg. 11526.99). I'd like to print it as 11,562.99 with a comma. How can I insert a comma in C? Try: #include <locale.h> #include <stdio.h> int main() { float f = 12345.67; // obtain the existing locale name for numbers char *oldLocale = setlocale(LC_NUMERIC, NULL); // inherit locale from environment setlocale(LC_NUMERIC, ""); // print number printf("%'.2f\n", f); // set the locale back setlocale(LC_NUMERIC, oldLocale); }

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

只愿长相守 提交于 2019-12-04 19:41:02
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 precision IEEE 32-bit floating-point is represented by 0 01111111 00000000000000000000000 where the first bit

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

你说的曾经没有我的故事 提交于 2019-12-04 08:49:20
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 x[] = { f2u(1.0), f2u(2.0), f2u(3.0), ... } The error I get is (logically): lvalue required as unary ‘

Half-precision floating-point arithmetic on Intel chips

拟墨画扇 提交于 2019-12-03 16:03:20
Is it possible to perform half-precision floating-point arithmetic on Intel chips? I know how to load/store/convert half-precision floating-point numbers [1] but I do not know how to add/multiply them without converting to single-precision floating-point numbers. [1] https://software.intel.com/en-us/articles/performance-benefits-of-half-precision-floats Is it possible to perform half-precision floating-point arithmetic on Intel chips? Yes, apparently the on-chip GPU in Skylake and later has hardware support for FP16 and FP64 , as well as FP32. With new enough drivers you can use it via OpenCL.

Size of int and float

我的未来我决定 提交于 2019-12-03 05:04:43
问题 I have a question about the ranges of ints and floats: If they both have the same size of 4 bytes, why do they have different ranges? 回答1: They are totally different - typically int is just a straightforward 2's complement signed integer, while float is a single precision floating point representation with 23 bits of mantissa, 8 bits exponent and 1 bit sign (see http://en.wikipedia.org/wiki/IEEE_754-2008). 回答2: They have different ranges of values because their contents are interpreted

Converting float to char*

雨燕双飞 提交于 2019-12-03 02:10:07
How can I convert a float value to char* in C language? Delan Azabani char buffer[64]; int ret = snprintf(buffer, sizeof buffer, "%f", myFloat); if (ret < 0) { return EXIT_FAILURE; } if (ret >= sizeof buffer) { /* Result was truncated - resize the buffer and retry. } That will store the string representation of myFloat in myCharPointer . Make sure that the string is large enough to hold it, though. snprintf is a better option than sprintf as it guarantees it will never write past the size of the buffer you supply in argument 2. char array[10]; sprintf(array, "%f", 3.123); sprintf : (from MSDN)

How to change a float64 number to uint64 in a right way?

梦想的初衷 提交于 2019-12-02 08:30:26
问题 package main func main() { var n float64 = 6161047830682206209 println(uint64(n)) } The output will be: 6161047830682206208 It looks like that when float64 change to uint64 , the fraction is discarded. 回答1: The problem here is the representation of constants and floating point numbers. Constants are represented in arbitrary precision. Floating point numbers are represented using the IEEE 754 standard. Spec: Constants: Numeric constants represent values of arbitrary precision and do not

How to change a float64 number to uint64 in a right way?

核能气质少年 提交于 2019-12-02 04:14:17
package main func main() { var n float64 = 6161047830682206209 println(uint64(n)) } The output will be: 6161047830682206208 It looks like that when float64 change to uint64 , the fraction is discarded. The problem here is the representation of constants and floating point numbers. Constants are represented in arbitrary precision. Floating point numbers are represented using the IEEE 754 standard. Spec: Constants: Numeric constants represent values of arbitrary precision and do not overflow. Spec: Numeric types: float64 the set of all IEEE-754 64-bit floating-point numbers In IEEE 754 the

Convert signed IEEE 754 float to hexadecimal representation

时间秒杀一切 提交于 2019-12-01 17:51:58
I'm using a front-end of Lua which is unfortunately outdated, so I'm stuck with version 5.1 here, meaning the bit32 library is out of reach (which I probably could have used to convert this). So I'm wondering if anyone knows of a way I could implement either a floating-point to binary (digits) function or, better yet, floating-point to hex. The best I've been able to come up with so far is a decimal to binary/hex function... The following functions uses some code from François Perrad's lua-MessagePack . A big thank you goes to him. function float2hex (n) if n == 0.0 then return 0.0 end local

Convert scientific notation to decimal - python

做~自己de王妃 提交于 2019-12-01 03:01:32
问题 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 回答1: The scientific notation is just a convenient way of printing a