Questions tagged [floating-point-conversion]

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

290 questions
179
votes
1 answer

Why do ARM chips have an instruction with Javascript in the name (FJCVTZS)?

FJCVTZS is "Floating-point Javascript Convert to Signed fixed-point, rounding toward Zero". It is supported in Arm v8.3-A chips and later. Which is odd, because you don't expect to see JavaScript so close to the bare metal. I can find explanations…
Tim Smith
  • 1,714
  • 2
  • 12
  • 14
70
votes
9 answers

Why does C++ promote an int to a float when a float cannot represent all int values?

Say I have the following: int i = 23; float f = 3.14; if (i == f) // do something i will be promoted to a float and the two float numbers will be compared, but can a float represent all int values? Why not promote both the int and the float to a…
user4344762
43
votes
2 answers

printf format float with padding

The following test code produces an undesired output, even though I used a width parameter: int main(int , char* []) { float test = 1234.5f; float test2 = 14.5f; printf("ABC %5.1f DEF\n", test); printf("ABC %5.1f DEF\n", test2); …
nabulke
  • 11,025
  • 13
  • 65
  • 114
40
votes
5 answers

Format a float to n decimal places and no trailing zeros

I want to display a float with the entire integer part and up to two decimals for the fractional part, without trailing zeros. http://play.golang.org/p/mAdQl6erWX: // Desired output: // "1.9" // "10.9" //…
Blaise
  • 13,139
  • 9
  • 69
  • 97
32
votes
5 answers

How to convert a float into hex

In Python I need to convert a bunch of floats into hexadecimal. It needs to be zero padded (for instance, 0x00000010 instead of 0x10). Just like http://gregstoll.dyndns.org/~gregstoll/floattohex/ does. (sadly i can't use external libs on my platform…
user2339945
  • 623
  • 1
  • 9
  • 14
31
votes
1 answer

Why does str(float) return more digits in Python 3 than Python 2?

In Python 2.7, repr of a float returns the nearest decimal number up to 17 digits long; this is precise enough to uniquely identify each possible IEEE floating point value. str of a float worked similarly, except that it limited the result to 12…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
28
votes
4 answers

How to convert a float string to an integer in python 3

I'm new to the whole coding thing...so here goes. Just trying to write a simple number guess game, but also do input validation. So that only integers are accepted as input. I've figured out how to weed out alphabetic characters, so I can convert…
23
votes
9 answers

Converting float to char*

How can I convert a float value to char* in C language?
boom
  • 5,856
  • 24
  • 61
  • 96
23
votes
12 answers

Best way of checking if a floating point is an integer

[There are a few questions on this but none of the answers are particularly definitive and several are out of date with the current C++ standard]. My research shows these are the principal methods used to check if a floating point value can be…
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
21
votes
6 answers

Size of int and float

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?
atul
  • 235
  • 1
  • 3
  • 6
20
votes
4 answers

how can I force division to be floating point in Go?

I have the following code snippet: package main import("fmt";"flag") func main() { var a = flag.Int("a",0,"divident") var b = flag.Int("b",1,"divisor") flag.Parse() fmt.Printf("%f",*a / *b ) } For -a 3 and -b 2 command line…
Croo
  • 1,301
  • 2
  • 13
  • 32
19
votes
2 answers

Half-precision floating-point arithmetic on Intel chips

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…
Kadir
  • 1,345
  • 3
  • 15
  • 25
18
votes
2 answers

What blocks implementation of std::to_chars and std::from_chars

According to https://en.cppreference.com/w/cpp/compiler_support#cpp17, no major vendor yet supports floating point versions of std::to_chars and std::from_chars. I understand that correctly formatting a floating point number is nontrivial, yet…
user877329
  • 6,717
  • 8
  • 46
  • 88
16
votes
1 answer

Will int to double conversion round up, down or to nearest double?

Simple question: Will the conversion from an int, say 100, to a double "round" up or down to the next double or will it always round to the nearest one (smallest delta)? e.g. for static_cast(100): Which way will it cast if d2 < d1? Bonus…
glades
  • 3,778
  • 1
  • 12
  • 34
15
votes
9 answers

Limit floating point precision?

Is there a way to round floating points to 2 points? E.g.: 3576.7675745342556 becomes 3576.76.
jmasterx
  • 52,639
  • 96
  • 311
  • 557
1
2 3
19 20