Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert integer to double implicitly?

Tags:

int a{5},b{2},c{9};
double d = (double)a / (double)b + (double)c;

Or I can use static_cast. Either way is verbose, especially when the formula is long. Is there a better solution?

like image 977
Milo Lu Avatar asked Jun 12 '16 17:06

Milo Lu


People also ask

Can I convert int to double in C++?

if i'm not wrong you can directly assign an integer to a double in c++, there's no need to do an explicit casting. It's called "typecasting". int a = 5; double b = (double) a; double c = 4.0; int d = (int) c; chandra.

How do I convert an int to a double in C#?

To convert a specified value to a double-precision floating-point number, use Convert. ToDouble() method. long[] val = { 340, -200}; Now convert it to Double.

How do you convert an int to a double in Python?

You need to cast one of the terms to a floating point value. Either explicitly by using float (that is ratio = float(a)/b or ratio=a/float(b) ) or implicitly by adding 0.0 : ratio = (a+0.0)/b . If using Python 2 you can from __future__ import division to make division not be the integral one but the float one.


2 Answers

You can multiply by 1.0:

int a{5}, b{2}, c{9};
double d = 1.0 * a / b + 1.0 * c;

And when you work with sums you can add to 0.0:

double d = 0.0 + a - b + c;

Most compilers perform optimization such that the meaningless operation is not evaluated. Only type conversion is done.


Remember that you only need to cast the first member in each division/multiply group. Do so in any manner that seems reasonable. And simple addition/substraction (with no other type multipliers/divisors) is casted too. Compilers guarantee casting. So your example:

double d = (double)a / (double)b + (double)c;

Really may be rewritten like this:

double d = (double)a / b + c;
double d = 1.0 * a / b + c;
double d = static_cast<double>(a) / b + c;

Some more examples:

double d = (double)a / b + (double)c / d + e;
double d = 1.0 * a / b + 1.0 * c / d + e;
double d = static_cast<double>(a) / b + static_cast<double>(c) / d + e;
like image 195
oklas Avatar answered Sep 20 '22 05:09

oklas


Is there a better solution?

Yes. Express intent through functions.

Marvel as the optimiser emits perfectly efficient assembler. Enjoy the accolades of your colleagues who gaze in wonder at your awesomely readable and maintainable code:

#include <iostream>

auto a_over_b_plus_c(double a, double b, double c)
{
  double d = a / b + c;
  return d;
}

int main()
{
  int a = 5, b = 2, c = 9;

  std::cout << a_over_b_plus_c(a, b, c) << std::endl;
}

For fun, here's a solution based on tuples & lambdas:

#include <iostream>
#include <tuple>

template<class T, class...Args> 
auto to(Args&&...args)
{
  return std::make_tuple(T(std::forward<Args>(args))...);
}

int main()
{
  int a = 5, b = 2, c = 9;

  auto calc = [](auto&& vals) {
    auto& a = std::get<0>(vals);
    auto& b = std::get<1>(vals);
    auto& c = std::get<2>(vals);

    return a / b + c;
  };

  auto result = calc(to<double>(a, b, c));

  std::cout << result << std::endl;
}

... and something perhaps more readable...

#include <iostream>
#include <tuple>
#include <complex>

template<class T, class F, class...Args> 
auto with(F f, Args&&...args)
{
  return f(T(std::forward<Args>(args))...);
}



int main()
{
  int a = 5, b = 2, c = 9;

  auto calc = [](auto&& a, auto&& b, auto&& c) {

    return a / b + c;
  };

  auto result = with<double>(calc, a, b, c);
  auto result2 = with<float>(calc, a, b, c);
  auto result3 = with<std::complex<double>>(calc, a, b, c);
  auto result4 = with<std::complex<float>>(calc, a, b, c);

  std::cout << result << std::endl;
  std::cout << result2 << std::endl;
  std::cout << result3 << std::endl;
}
like image 22
Richard Hodges Avatar answered Sep 23 '22 05:09

Richard Hodges