473,624 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8713
Mike P <mr*@telcoelect ronics.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.co m>
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*@telcoelect ronics.co.uk> wrote in message
news:uW******** *****@TK2MSFTNG P11.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)(fltPCRa teStd * 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*@telcoelect ronics.co.uk> wrote in message
news:uW******** *****@TK2MSFTNG P11.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******** ***********@new s4.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*@telcoelect ronics.co.uk> wrote in message
news:uW******** *****@TK2MSFTNG P11.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)(fltPCRa teStd * 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
2561
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 java: 325 gcc: 348 Python with Psyco: 1317 Pure Python using sum: 2312
43
2185
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 what it is and where to look for help, maybe some of you know? double=float*double; (or every possible combination of it). An example: 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
3
1441
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 individually? or would grouping the "digits" in the string by say 10 or 20 and working the smaller parts? Would i loose something by grouping in that way? Or is there a faster/better way? I have been thinking about this for a while and I don't want to start...
14
1674
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 floats or doubles incremented that way before. Just to be sure I did a toy program and compiled with gcc -ansi -pedantic -Wall and it worked without any error or warning, showing the correct result. Is it correct to increment floats or doubles with...
25
7877
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 to use the shift operator on unsigned longs without any problems. does anyone know how to do this properly?
7
3872
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
6844
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) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a))) Do the same issues involved in comparing 2 fp types for equality apply to comparing a float to zero? E.g. is if(x == 0.0) considered harmful?
6
2902
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 in IEEE 754 with reasonably optimal precision and performance? The purpose is to exchange serialized doubles and floats between C/C++ and Java programs where Java serialization rules are used.
3
1924
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
0
8614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.