Questions tagged [floating-point-conversion]

Anything related to converting a floating point number to and from other representations.

290 questions
2
votes
3 answers

An Alternative to Floating-Point for Storing Simple Fractional Values

Firstly, the problem I'm trying to solve is coming up with a better representation for values that will always remain uniformly distributed in the range: 0.0 <= x < 1.0 The motivation for this is to attempt to reduce the number of bytes used to…
Dan
  • 33,953
  • 24
  • 61
  • 87
2
votes
6 answers

why float is not working?

in my code, the output is 2.000000 whereas it's supposed to be 2.11111 #includemain(){ int i,j; float r; i = 19; j = 9; r = i / j; printf(" %f ",r);} why it's not working?
user2779715
  • 33
  • 1
  • 4
2
votes
1 answer

sscanf to float using c

So I have a file that has multiple strings. I'm supposed to use fgets to read each line then use sscanf to break the string up and process them into my struct. Here's an example. 38L Lee, Victor; 2.8 The first is the id, second is name, and finally…
Nathan
  • 483
  • 4
  • 7
  • 19
2
votes
2 answers

scanf() and floating point numbers in Turbo C

I created a structure as: typedef struct { float real, img; } cmplx; And I created a function as void input(cmplx *a) { scanf("%f + %f i", &a->real, &a->img); } and called the function from main as: cmplx a; input(&a); The execution stops…
cipher
  • 2,414
  • 4
  • 30
  • 54
2
votes
5 answers

Extract double from a string in c

I want to write a function in C that has one string parameter and returns a double number. For example when the string is fsldnf213414fasfa it should return 213414. But it should be also able to return floating points like fasfasf123.412412fasfff as…
1
vote
1 answer

When converting from decimal to binary floating point values, is rounding used, or truncation?

So, I am trying to understand floating point operations better, and I understand that when arithmetic operations are performed (I'm looking primarily at RISC) a rounding, guard and sticky bit are used for rounding of the result during…
1
vote
3 answers

How to express float constants precisely in source code

I have some C++11 code generated via a code generator that contains a large array of floats, and I want to make sure that the compiled values are precisely the same as the compiled values in the generator (assuming that both depend on the same float…
Cerno
  • 751
  • 4
  • 14
1
vote
1 answer

Parsing fixed width numbers with boost spirit

I'm using spirit to parse fortran-like text file filled with fixed width numbers: 1234 0.000000000000D+001234 1234 7.654321000000D+001234 1234 1234 1234-7.654321000000D+001234 There are parsers for signed and unsigned integers,…
1
vote
1 answer

Why does GCC use "mov edi, edi" for this unsigned integer to double conversion but not for signed?

When compile(x86-64 gcc 11.2 with -O2) the code below: double foo(unsigned int x) { return x/2.5; } double zoo(int x) { return x/2.5; } int main(){ return 0; } I got the assembly like this: foo(unsigned int): mov edi,…
konchy
  • 573
  • 5
  • 16
1
vote
2 answers

Floating-point-to-integer conversion rounding up instead of truncating

I was surprised to find that a floating-point-to-integer conversion rounded up instead of truncating the fractional part. Here is some sample code, compiled using Clang, that reproduces that behavior: double a = 1.12; // 1.1200000000000001 *…
fumoboy007
  • 5,345
  • 4
  • 32
  • 49
1
vote
1 answer

Finding out whether conversion from decimal to binary floating point is exact, rounding up, or rounding down

I know that values that can be exactly represented in decimal floating point are often unable to be represented as exact values in binary floating point. It is easy to demonstrate in e.g. python a = float("0.5") print('%.17e' %…
Bill Sellers
  • 436
  • 3
  • 9
1
vote
2 answers

How to create a custom NaN (single precision) in python without setting the 23rd bit?

I'm trying to create floating-point NaNs by choosing the fraction bits. But it seems that python float always set the 23rd fraction bit (IEEE754 single) when it interprets a NaN. So, my question is: is it possible to define a float nan in python…
R T
  • 13
  • 3
1
vote
1 answer

floating point rounding errors

The output is x=1000300 y=1000000, z=1000300 I can understand how I got x and z but c's y's output makes no sense. #include int main() { int i=0; float a = 100; a = a*a*a*a*a; float c = 3; float x = 1000000*c + a; …
foo
  • 2,241
  • 6
  • 21
  • 26
1
vote
1 answer

How to get value of symbolic expression for further processing

When using symbolic expression, the question is how to convert symbolic value to discrete value and obtain z value. x=2 syms x y=x^2 z=x+y
1
vote
1 answer

Why does conversion to extFloat80_t always return 0?

I've constructed what I believe to be the simplest possible use of the extFloat80_t type provided by the SoftFloat library (http://www.jhauser.us/arithmetic/SoftFloat.html) and can't see what I'm doing wrong -- all values for i32 that I've tried…
gmabey
  • 15
  • 1
  • 6