Anything related to converting a floating point number to and from other representations.
Questions tagged [floating-point-conversion]
290 questions
3
votes
3 answers
Casting double to integer when is it undefined behaviour
So following:
double t = 244.233;
int a = (int) t;
is not undefined behaviour since 244 can fit inside int did I get it right?
Otherwise if it was larger value instead of 244 which didn't fit inside int this would be undefined, did I get it…
user5012412
3
votes
0 answers
fp32 to fp16 and reverse conversion - Matlab or C++ code
I have a decimal number (such as 0.15625) which is stored as 32-bit floating point (fp32) number (0-01111100-01000000000000000000000 (sign-exponent-mantissa)) in memory. I want to convert that fp32 to fp16, and then show it in decimal. I guess the…

ard24ie
- 181
- 3
- 12
3
votes
1 answer
how does Florian's Grisu2 algorithm work?
I come across a problem about converting double to ascii, after searching, I got Florian's paper "Printing Floating-Point Numbers Quickly and Accurately with Integers", Grisu2 algorithm is really awesome and much faster. I have understood Grisu2's…

user1024
- 982
- 4
- 13
- 26
3
votes
1 answer
What are the maximum number of values representable for different ranges within the floating-point representation?
I'm trying to figure out some specifics of using a normalized representation for floating-point values.
As I understand, the IEEE-754 representation works like:
1.{significand} * 2^{exponent}
For single precision, the significand being 24 bits, the…

Dan
- 33,953
- 24
- 61
- 87
3
votes
1 answer
Is rounding behavior of string-to-double methods defined?
Ideally, a string-to-double method would always yield the double whose value was closest to the exact numerical value of the specified string; for example, since "102030405060708072.99" is only 7.01 away from the next larger value, but 8.99 away…

supercat
- 77,689
- 9
- 166
- 211
3
votes
6 answers
How to check if a user input is a float
I'm doing Learn Python the Hard Way exercise 35. Below is the original code, and we're asked to change it so it can accept numbers that don't have just 0 and 1 in them.
def gold_room():
print "This room is full of gold. How much do you take?"
…

pez
- 3,859
- 12
- 40
- 72
3
votes
1 answer
Handling endianness of floating point values when there is no fixed size floating point type available
I'm writing a binary file reader/writer and have decided that to handle the issue of endianness I will convert all data to "network" (big) endianness on writing and to host endianness on reading. I'm avoiding hton* because I don't want to link with…

Peter Clark
- 2,863
- 3
- 23
- 37
3
votes
1 answer
Converting floating point number to any base
How would i convert a float, represented by a string, to a decimal, base between 2 to 36 in Python, without using Python built ins int and float?
meaning:
convert_float("234.56", base) --> float, or ("10AB", base) --> float
In case that the float…

nanachan
- 1,051
- 1
- 15
- 26
3
votes
3 answers
Converting unsigned to double to unsigned without loss of precision
Will an integer value converted to a floating-point value and back again be the same as the original integer value?
For example:
unsigned x = 42;
double y = x;
unsigned z = y;
Assuming the compiler doesn't optimize out the floating-point…

Vilhelm Gray
- 11,516
- 10
- 61
- 114
3
votes
3 answers
Correct algorithm to convert binary floating point "1101.11" into decimal (13.75)?
I have written a program in C to convert a floating point number represented in binary (1101.11) into a decimal (13.75).
However, I cannot seem to get the correct value out of the algorithm.
What is the correct method for converting a binary…

biswajit
- 2,707
- 4
- 17
- 16
3
votes
1 answer
Float value confusion in C
My code is
void main()
{
float a = 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
It prints C and this is fine as a treated as double constant value and its value will be 0.699999 which is less than 0.7.
Now if…

John
- 159
- 11
2
votes
6 answers
Reading floats into an array
How could I read let's say 10 floats and store them in an array without wasting any memory?

user104108
- 139
- 1
- 2
- 7
2
votes
0 answers
How to efficiently perform double/int128 conversions with AVX2?
I'm trying to make a software that users can move in a wide range(at least 1Mly diameter range and at least 0.1mm position representation precision). I think of 128bit fixed point number to represent position. However, mathematical calculation(e.g.…

Velctor
- 21
- 4
2
votes
1 answer
Cortex-M7: What's the most efficient way to convert a 64-bit unsigned integer to a single-precision floating point number in assembler?
When I want to convert a 32-bit unsigned integer (e.g. residing in register r0) to a single-precision floating-point number for the VFP (e.g. to be stored in s0), I use:
vmov.f32 s0, r0
vcvt.f32.u32 s0, s0
However, surprisingly (to me at…

Hansel
- 233
- 1
- 9
2
votes
4 answers
Assigning a float value to variable
I want to store 1.222 in the variable a.But when i print it is showing me 1.222000 which
means that the variable a is stored as 1.222000.But i want to store the value as 1.222 in
a. and also i want only 1.22 to be copied to b when assign a=b.…

Angus
- 12,133
- 29
- 96
- 151