Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which numeric type conversion is better for simple math operation?

I want to know which conversion is better (regarding performance/speed and precision/most less loss) for a simple math operation and what makes them different?

Example:

double double1 = integer1 / (5 * integer2);
var double2 = integer1 / (5.0 * integer2);
var double3 = integer1 / (5D * integer2);
var double4 = (double) integer1 / (5 * integer2);
var double5 = integer1 / (double) (5 * integer2);
var double6 = integer1 / ((double) 5 * integer2);
var double7 = integer1 / (5 * (double) integer2);
var double8 = Convert.ToDouble(integer1 / (5 * integer2));
var double9 = integer1 / Convert.ToDouble(5 * integer2);

Actually my question is about the conversion not the type itself.

like image 286
MagB Avatar asked May 09 '16 07:05

MagB


People also ask

What is implicit type conversion?

In Implicit type conversion, Python automatically converts one data type to another data type. This process doesn't need any user involvement. Let's see an example where Python promotes the conversion of the lower data type (integer) to the higher data type (float) to avoid data loss.

What are the different types of conversion?

There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting.

What is type conversion explain with example?

Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string.

What is the use of type conversion write the standard conversion rules in C ++?

A conversion produces a new value of some type from a value of a different type. Standard conversions are built into the C++ language and support its built-in types, and you can create user-defined conversions to perform conversions to, from, or between user-defined types.


1 Answers

EDIT

In response to your totally changed question:

The first line double double1 = integer1 / (5 * integer2); does an integer division, so don't do that.

Also the line var double8 = Convert.ToDouble(integer1 / (5 * integer2)); is doing integer division before converting the result to a double, so don't do that either.

Other than that, all the different approaches you list will end up calling the IL instruction Conv.R8 once for each line in your sample code.

The only real difference is that Convert.ToDouble() will make a method call to do so, so you should avoid that.

The results for every line other than double1 and double8 will be identical.

So you should probably go for the simplest: var double2 = integer1 / (5.0 * integer2);

In a more complicated situation, time your code to see if there's any differences.

like image 95
Matthew Watson Avatar answered Oct 01 '22 19:10

Matthew Watson