By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,701 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

doubles and floats

P: n/a
Why does this cause the error 'cannot implicitly convert type 'double'
to 'float'? Can you not multiply doubles by floats without converting
them all to the same datatype?

fltPCRateStd = fltPCRateStd * 1.175;
Regards,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Mike P <mr*@telcoelectronics.co.uk> wrote:
Why does this cause the error 'cannot implicitly convert type 'double'
to 'float'? Can you not multiply doubles by floats without converting
them all to the same datatype?

fltPCRateStd = fltPCRateStd * 1.175;


1.175 is implicitly a double, and double*float results in a double. If
you do

fltPCRateStd = fltPCRateStd * 1.175f;
or
fltPCRateStd *= 1.175f;

it should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
It requires an implicit conversion because double to float conversion is
potentially unsafe
and might throw an exception.
In your case, you may change
fltPCRateStd = fltPCRateStd * 1.175;
into
fltPCRateStd = fltPCRateStd * 1.175F;
to explicitly specify that the number 1.175 is of single precision.
Floating point numbers defaults to be double precisioned.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:uW*************@TK2MSFTNGP11.phx.gbl...
Why does this cause the error 'cannot implicitly convert type 'double'
to 'float'? Can you not multiply doubles by floats without converting
them all to the same datatype?

fltPCRateStd = fltPCRateStd * 1.175;
Regards,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3

P: n/a
The expression on the right-hand side generates a value of type double. This
cannot be assigned to a float without casting. So,

Option1: cast the whole expr to float: (float)(fltPCRateStd * 1.175)
Option2: Suffix 'f' for the literal value so that the expr results in a
float type value which can be assigned to a float type var: fltPCRateStd *
1.175f

HTH

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:uW*************@TK2MSFTNGP11.phx.gbl...
Why does this cause the error 'cannot implicitly convert type 'double'
to 'float'? Can you not multiply doubles by floats without converting
them all to the same datatype?

fltPCRateStd = fltPCRateStd * 1.175;
Regards,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

P: n/a
Sorry;
"It requires an implicit conversion"
should be
"It requires an explicit conversion"

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:7a*******************@news4.e.nsc.no...
It requires an implicit conversion because double to float conversion is
potentially unsafe
and might throw an exception.
In your case, you may change
fltPCRateStd = fltPCRateStd * 1.175;
into
fltPCRateStd = fltPCRateStd * 1.175F;
to explicitly specify that the number 1.175 is of single precision.
Floating point numbers defaults to be double precisioned.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:uW*************@TK2MSFTNGP11.phx.gbl...
Why does this cause the error 'cannot implicitly convert type 'double'
to 'float'? Can you not multiply doubles by floats without converting
them all to the same datatype?

fltPCRateStd = fltPCRateStd * 1.175;
Regards,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #5

P: n/a
Hi Mike,

Multiplying a double with a float will upgrade the float to a double and the result will be a double.
When you are trying to store this result in fltPCRateStd you get this error because float cannot contain all data that may be found in a double.

1.175 without any type specifier will automatically be a double. Specify float to fix it.

fltPCRateStd = fltPCRateStd * 1.175f;
fltPCRateStd = (float)(fltPCRateStd * 1.175);
fltPCRateStd *= 1.175f;

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6

P: n/a
Thanks everybody,

I realised pretty soon after I posted that float = float * double is
potentially unsafe. Since I'm using Math.Round (which only accepts
doubles and decimals) on the result of the calculation, I've decided to
convert the floats to doubles, which makes things a lot more
straightforward.
Cheers,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.