Oliver Apel
Oliver Apel

Reputation: 1878

multiplication and division with decimals

I´m new to C# and have now a Problem with mathematical stuff. Why can´t I mulitplicate or divide "simple" like this:

 decimal mLon1 = 0;
 decimal mLat1 = 0;
 decimal mFactor = 111021;
 decimal mRadius = 100;
 decimal mLat = 12.123;

 mLon1 = mLon - mRadius / Math.Abs(Math.Cos(((Math.PI / 180) * mLat)) * mFaktor);
 mLat1 = mLat - (mRadius / mFaktor);

Compiler Shows an error:

The Operator '*' can not be applied to operands of type 'double' and 'decimal'.

Same with Operator '/' ...

How can I get my code workin? Thx for any hint.

Upvotes: 5

Views: 7586

Answers (5)

Jerric Lyns John
Jerric Lyns John

Reputation: 936

C# is a type-safe language, what i meant by type safe is that the code allows the developer to be certain that a value or object will be of a certain type so that he/she can use it in a specific way without fear of unexpected or undefined behavior. C# only some implicit conversion of types like int to double

Change your code as shown below

decimal mLon1 = 0M;
decimal mLat1 = 0M;
decimal mFactor = 111021M;//mFactor is never used
decimal mRadius = 100M;
decimal mLat = 12.123M;

mLon1 = mLon - mRadius / Math.Abs(Convert.ToDecimal(Math.Cos((Math.PI / 180) * Convert.ToDouble(mLat))) * mFaktor);//mFaktor and mLon is not defined
mLat1 = mLat - (mRadius / mFaktor);

Here Convert.ToDouble(mLat) converts mLat to double since C# doesn't allow implicit conversion of decimal to double, why am I doing this? Well Math.Cos apparently accepts only double, so is the case of Math.Abs

Another workaround is that you could convert every declared variables as double and reduce the pain of explicit type conversions in between

double mLon1 = 0D;
double mLat1 = 0D;
double mFactor = 111021D;//mFactor is never used
double mRadius = 100D;
double mLat = 12.123D;

mLon1 = mLon - mRadius / Math.Abs(Math.Cos(((Math.PI / 180D) * mLat)) * mFaktor);//mFaktor and mLon is not defined
mLat1 = mLat - (mRadius / mFaktor);

Please see the below mentioned reference and what type is that you want to use in the present scenario that you are facing.

Floating point reference

Reference: MSDN

Upvotes: 4

Sebastien Guimmara
Sebastien Guimmara

Reputation: 175

You shouldn't use the decimal type for this usage. Use the double type and everything will be fine.

In your case, the math function returns a double precision floating point (64 bits) number and the decimal type is a 128 bits floating point non-native data type.

You can't directly multiply two numbers that don't have the same memory size.

Upvotes: 2

Neel
Neel

Reputation: 11741

try below code and you have spelling mistake near "mFaktor" which should be "mFactor"

decimal mLon1 = 0;
decimal mLat1 = 0;
decimal mFactor = 111021;
decimal mRadius = 100;
decimal mLat = (decimal)12.123;
decimal mLon = 0;
mLon1 = mLon - mRadius / (decimal)Math.Abs(Math.Cos(((Math.PI / 180) * (double)mLat)) * (double)mFactor);
mLat1 = mLat - (mRadius / mFactor);

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98868

First of all, your code doesn't even compile.

If you want to use 12.123 as a decimal, you need to use m or M suffix. If you don't use any suffix with your floating type, C# thinks it is a double.

From decimal (C# Reference)

If you want a numeric real literal to be treated as decimal, use the suffix m or M. Without the suffix m, the number is treated as a double and generates a compiler error.

And there are no implicit conversions between floating-point types (float and double) and the decimal type.

Since Math.PI field represents double, your Math.PI / 180 will be double. Since mLat is decimal (you said it is a spelling mistake) you try to get double * decimal which is clearly you get these error. You try to same operation with / operator also.

From C# Spec $7.7.1 Multiplication operator

float operator *(float x, float y);
double operator *(double x, double y);
decimal operator *(decimal x, decimal y);

And from C# Spec $7.7.2 Division operator

float operator /(float x, float y);
double operator /(double x, double y);
decimal operator /(decimal x, decimal y);

As you can see from these documentations, there is no defined double * decimal or double / decimal operations.

Upvotes: 2

Codor
Codor

Reputation: 17605

You could also use variables of type double instead of decimal.

Upvotes: 0

Related Questions