0

Let's assume a declared variable float Price. I want to do Price = Price + 3;

Is it more efficient (for compiling time) to do Price = Price + 3.0;?

My understanding is that constants have types that are detected during lexical analysis, and when the operation is detected during compilation, if the types are not compatible, the compiler will need to do a lossless conversion for the right operands. I don't think there will be an int-to-float conversion in the assembler code, since the conversion can be done during compiling, but is there an int-to-float conversion during compiling time (implying more time in processing)?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Lau
  • 1
  • 2
    technically it would depend on the compiler, one would hope that even with no optimization the compiler would do the conversion at compile time and not run time. What did you see when you tried it? – old_timer Jul 14 '20 at 15:42
  • 2
    Depends entirely on the compiler, but I'd be surprised at a compiler that didn't do that conversion. You can conveniently test different compilers [here](https://godbolt.org/) – john Jul 14 '20 at 15:42
  • Technically it is a conversion that has to be done yes, and it could be done runtime if the compiler chose to do that. – old_timer Jul 14 '20 at 15:43
  • 7
    Incidentally you should not be [using float variables to represent money](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). – john Jul 14 '20 at 15:45
  • 1
    The compiler has to convert every constant in your program from a textual representation to the corresponding internal representation, which is much more expensive than converting between int and float (or double, which is what you should be using by default). The int->float conversion won't be noticeable. – rici Jul 14 '20 at 15:53
  • `Is it more efficient (for compiling time)` the difference for a single value should be negligible. Anyway testing with initializing [a 100000 static const array](https://repl.it/@kamilcukrowski/MonthlyRightJumpthreading#main.sh) compiles `a[] = {1,2,3...}` faster then `a[] = { 1.0,2.0,3.0 ...}` on repl with gcc7 – KamilCuk Jul 14 '20 at 15:58
  • Very cool link @john. Had not tested before, and i am not 100% sure how the assembler notation works, but there doesnt seem to be any difference, except for the data section of the assembler code, as was expected. – Lau Jul 14 '20 at 16:05
  • 2
    Did `:=` become an assignment operator in C++ while I wasn't looking? :) – Jeremy Friesner Jul 14 '20 at 16:11
  • @Lau For example in this [code](https://godbolt.org/z/aK3zMW) the data `.long 1077936128` is the integer literal `3` converted to a float constant. That number being the binary representation of `3.0f`. – john Jul 14 '20 at 16:38
  • No one's going to point out that if they wanted a float, they have to type `3.0f`? 3.0 alone is a double, so OP would still be causing a conversion to happen. – sweenish Jul 14 '20 at 16:47
  • @sweenish I was just about to, but you beat me to it. – Remy Lebeau Jul 14 '20 at 18:19

0 Answers0