Anything related to converting a floating point number to and from other representations.
Questions tagged [floating-point-conversion]
290 questions
2
votes
4 answers
Float value parsing in java
When I am doing this
value = Float.parseFloat("5555998.558f");
System.out.println("Value: " + value);
It gives me result 5555998.5 rather than 5555998.558
I am using the variable value for some calculation. And I need the exact value i.e.…

Biswadip Dey
- 509
- 2
- 7
- 20
2
votes
1 answer
Rounding point issues when converting to float bitwise
I am working on a homework assignment, where we are supposed to convert an int to float via bitwise operations. The following code works, except it encounters rounding. My function seems to always round down, but in some cases it should round…

jamiees2
- 176
- 1
- 8
2
votes
3 answers
How do I parse floats and doubles case-insensitively in c# (to cover "infinity", etc.)?
In my locale, double.Parse("Infinity") returns double.PositiveInfinity, but double.Parse("infinity") throws System.FormatException. Analogous things happen for negative infinity and NaN.
I could obviously just use…

Jude Melancon
- 462
- 5
- 11
2
votes
2 answers
Is it possible to assign char pointer to float value?
I tried to execute the below code in Visual Studio:
int main()
{
float f1 = 15.23;
char *ptr = &f1;
printf("%c", *(ptr));
getch();
return 0;
}
Compilation Error: error C2440: 'initializing' : cannot convert from
'float…

kapilddit
- 1,729
- 4
- 26
- 51
2
votes
2 answers
C++ / Python Equivalent of Matlab's num2hex
Is there a function in C++ or Python that is equivalent to MATLAB's num2hex function?
Python's float.hex and int.hex functions do not give the same result as MATLAB's num2hex function.

Armin Meisterhirn
- 801
- 1
- 13
- 26
2
votes
1 answer
C# BitConverter.ToSingle equivalent in Python
I have some code in C# that converts bytes to float using BitConverter.ToSingle function, like this:
float tmp_float = BitConverter.ToSingle(jobBff, cnt);
As I found out from this…

Vini.g.fer
- 11,639
- 16
- 61
- 90
2
votes
1 answer
Why is the mean smaller than the minimum and why does this change with 64bit floats?
I have an input array, which is a masked array.
When I check the mean, I get a nonsensical number: less than the reported minimum value!
So, raw array: numpy.mean(A) < numpy.min(A). Note A.dtype returns float32.
FIX: A3=A.astype(float). A3 is still…

user2701830
- 21
- 2
2
votes
3 answers
Reading float with C++ input operator
Consider
float num;
cin >> num;
Is it well defined how many characters of the input this code can consume. I am particularly interested in the case where the input stream may have the num specified in a much higher precision than what the float…

san
- 4,144
- 6
- 32
- 50
2
votes
4 answers
Why does a float fit in a double but a float[] doesn't fit a double[]?
If I have a function that takes a double as an argument, I can easily put in a float.
However when I have a funcion that takes a double[] then I cannot pass a float[].
public static void doSomethingWithMyDoubles(double[] doubles) {
…

Timo
- 2,212
- 2
- 25
- 46
2
votes
6 answers
How to convert a String to a Float character by character using Java?
I was asked this on an interview a while back and couldn't figure it out. I wasn't allowed to cast the entire thing at once so my next idea was to just run through the string converting until the point but the guy interviewing me told me he wanted…

Tsundoku
- 9,104
- 29
- 93
- 127
2
votes
1 answer
suppress "minus zero" from cout
double value;
std::ostringstream s;
s << std::fixed << std::setprecision(3) << value;
When value wanders in the range -1.0e-14 to 1.0e-14, s flickers between "0.000" and "-0.000".
Is there a clean way to suppress the minus sign, which only…

Camille Goudeseune
- 2,934
- 2
- 35
- 56
2
votes
1 answer
assumption on floating point conversion: (int)(float)n == n
May I assume that (int)(float)n == n for any int n? At least I need this for non-negative, 31 bits values.
addendum. What about (int)(double)n ==n?

Emanuele Paolini
- 9,912
- 3
- 38
- 64
2
votes
4 answers
Converting a Single To a Double
I am having an issue when converting Single values to Double values.
The singles provided by a BitStream are simple 2 to 6 place decimal numbers, in many cases, as simple as 0.4, 0.94, 0.6, etc. (I should note, the documentation I received states…

Jeremy
- 584
- 5
- 15
2
votes
1 answer
Is floating point math (on integers) accurate?
I have a library that modifies an input (adding or multiplying the input with one or more stored variables). These variables are stored as floats. Usually, the input is also a float, but in some cases it's an int. I'm concerned about the accuracy of…

piojo
- 6,351
- 1
- 26
- 36
2
votes
2 answers
Printing floating point value
I have this code sample:
float approx = 1234567712f;
System.out.println(approx);
Instead of printing "1.234567712E9" or something similar it prints "1.23456768E9". As I understand, this has something to do with precision at the binary level.
How…

LiTTle
- 1,811
- 1
- 20
- 37