Anything related to converting a floating point number to and from other representations.
Questions tagged [floating-point-conversion]
290 questions
6
votes
8 answers
Convert float to unsigned long to access float internals, in c #define
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…

Arnaud Gouder de Beauregard
- 1,529
- 1
- 13
- 22
6
votes
2 answers
How to convert postgres column type from string to float
I have a rails app with a basic postgres db, but I realized that some of my columns are strings and it'd be best if they were floats. I'm trying to convert columns for latitude and longitude from varchar to floating-point.
I've tried this post…

tsiege
- 468
- 1
- 4
- 17
6
votes
3 answers
convert int to float to hex
Using scanf, each number typed in, i would like my program to
print out two lines: for example
byte order: little-endian
> 2
2 0x00000002
2.00 0x40000000
> -2
-2 0xFFFFFFFE
-2.00 0xC0000000
I can get it to print out the 2 in hex
but i…

Steller
- 308
- 2
- 8
- 26
6
votes
6 answers
Are there unsigned equivalents of the x87 FILD and SSE CVTSI2SD instructions?
I want to implement the equivalent of C's uint-to-double cast in the GHC Haskell compiler. We already implement int-to-double using FILD or CVTSI2SD. Is there unsigned versions of these operations or am I supposed to zero out the highest bit of the…

tibbe
- 8,809
- 7
- 36
- 64
5
votes
5 answers
string format for printf floating point values
I have a question about using printf.
char str[8];
float val = 2.334563;
sprintf(str, format, val);
printf("val = %s.\n", str);
val = -23.34563;
sprintf(str, format, val);
printf("val = %s.\n", str);
val = -0.02334563;
sprintf(str, format,…

user1020803
- 59
- 1
- 3
5
votes
2 answers
Can float be round tripped via double without losing precision?
If I have a C# float, can I convert it to double without losing any precision?
If that double were converted back to float, would it have exactly the same value?

Drew Noakes
- 300,895
- 165
- 679
- 742
5
votes
1 answer
How to determine if a Javascript Number is in single-precision range?
According to the ECMAScript specification, Javascript number values correspond a to double-precision 64-bit binary format IEEE 754 value.
For a WebIDL validator I'm currently working on, I need to be able to figure out if a given number value can be…

Robby Cornelissen
- 91,784
- 22
- 134
- 156
5
votes
1 answer
Compare float array as int array
I need a optimized binary search algorithm on a array of sorted numbers. I did this and found that using float to store numbers is faster than using integer, because at the end i must calculate…

fangzhangmnm
- 71
- 8
5
votes
1 answer
Why does strtof not print proper float?
#include
#include
#include
int main(){
char aaa[35] = "1.25";
char* bbb = &(aaa[0]);
char** ccc = &(bbb);
float a = strtof(*ccc, ccc);
printf("%f\n", a);
return 0;
}
The code I wrote above should print…

John Luke
- 53
- 2
5
votes
2 answers
cast 32bit-float to 64bit-double on system where sizeof double == sizeof float == 4
I am trying to serialize a float according to the BSON spec which only has support for 64bit double. so i need to cast my float to a double.
On a system where sizeof(double) == 8 i would just do
float f = 3.14;
serialize((double)f);
but since…

FnuGk
- 63
- 6
5
votes
3 answers
Swift: extract float from byte data
I'm looking for a robust and elegant way to extract four big-endian bytes from an array as a Float.
I can get a UInt32 with the bits via something like this:
let data: [Byte] = [0x00, 0x00, 0x00, 0x40, 0x86, 0x66, 0x66, 0x00]
let dataPtr =…

NickHowes
- 133
- 2
- 14
5
votes
6 answers
C floating point precision
Possible Duplicate:
Floating point comparison
I have a problem about the accuracy of float in C/C++. When I execute the program below:
#include
int main (void) {
float a = 101.1;
double b = 101.1;
printf ("a: %f\n", a);
…

Jeremy
- 4,797
- 1
- 18
- 26
4
votes
2 answers
Out-of-range floating point to integer conversion breaks in VS2022 executable when linking VS2017 or VS2019 libraries
I have a snippet of code that's present in a VS2017 static library. When linked in a 2017 executable, it works as expected. However, if linking into a 2022 executable, it breaks on the cast from double to uint64_t.
The double is below -1.0 so the…

Robert Joseph Dacunto
- 451
- 2
- 5
- 17
4
votes
0 answers
Why is there a difference in calculation results between literals and variables?
When debugging some C code, I encountered some weird inconsistencies in double calculations. I managed to isolate it to the following minimal example:
#include
#include
int main()
{
float f = 268.6f;
printf("Using variable…

Zaroth
- 568
- 7
- 19
4
votes
1 answer
CVTTSD2SI - a truncating instruction - uses rounding with "inexact" results?
According to the Intel documentation the instruction CVTTSD2SI, which truncates a fp-value to a 64 bit signed integer, rounds according to the MXCSR rounding-mode if the conversion is inexact.
But how can a simple truncating conversion be inexact? I…

Bonita Montero
- 2,817
- 9
- 22