473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

doubles and floats

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
6 8701
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
43
by: J.K. Becker | last post by:
Hi there, I am trying to multiply doubles with floats (actually I tried every possible combination by now) and it never works (well, it does something but it is always wrong). I have no idea...
3
by: dave | last post by:
I'm trying to learn this. My problem, I have 2 strings that represent floats. I want to add them together. Should I look at using Cint to convert each character in the string and add...
14
by: my.correo.basura | last post by:
Yesterday I found in a piece of code a float being incremented with ++. For some reason (wrong, I guess...) I thought that only integer types could be incremented that way, and I had never seen...
25
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able...
7
by: SpreadTooThin | last post by:
I have some code... import array a = array.array('d') f = open('file.raw') a.fromfile(f, 10) now I need to convert them into floats (32 bit...) what do i do?
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
6
by: Pavel | last post by:
Hello, Does anyone know a (preferably open-source) multi-platform C or C++ library that would be able to write and read C/C++ doubles and floats to/from streambuf, char array or similar device...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.