473,395 Members | 1,629 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,395 software developers and data experts.

Newb float casting question

Are the following two similar?

float a = 1.0f;

float a = static_cast<float>(1.0);

If they are, which one is preferrable?

Thanks
Thierry

Sep 19 '05 #1
5 1339
la********@gmail.com wrote:
Are the following two similar?

float a = 1.0f;

float a = static_cast<float>(1.0);

If they are, which one is preferrable?


Yes, they are, AFAIUI.

I prefer the first, it's so much less to type or to read/comprehend.

If given a choice I'd even shorten it to

float a = 1;

Although it's basically the same as

float a(1);

, I still find the form with '=' easier on my eyes/brain.

V
Sep 19 '05 #2
<la********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Are the following two similar?

float a = 1.0f;
That one initializes 'a' with a value having the exact type.
float a = static_cast<float>(1.0);
That one uses a double, converts it to a float, then uses the result to
initialize a.
If they are, which one is preferrable?


The compiler would probably generate equivalent code for both, but I would
prefer the first one, because it is exactly what is intended, and the second
one is as strange as this:

float a = 2.0f / 2; // why? :)

Ali
Sep 19 '05 #3
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:NO*******************@newsread1.mlpsca01.us.t o.verio.net...
la********@gmail.com wrote:
float a = 1.0f; float a = static_cast<float>(1.0); If they are, which one is preferrable?

Yes, they are, AFAIUI. I prefer the first, it's so much less to type or to read/comprehend.


There is a much more subtle (additional) argument in favor of the first one.

The literal 1.0 has type double. So if you write

float a = static_cast<float>(1.0);

or, equivalently,

float a = 1.0;

you are taking the exact decimal value 1.0, converting it to double (with
implied rounding), and then converting it to float (with a second implied
rounding).

If you write

float a = 1.0f;

you are taking the exact decimal value 1.0 and converting it directly to
float, with only one rounding operation.

Rounding once is never less accurate than rounding twice, and might be more
accurate.
Sep 19 '05 #4
"Andrew Koenig" <ar*@acm.org> wrote in message
news:ci*******************@bgtnsc04-news.ops.worldnet.att.net

There is a much more subtle (additional) argument in favor of the
first one.
The literal 1.0 has type double. So if you write

float a = static_cast<float>(1.0);

or, equivalently,

float a = 1.0;

you are taking the exact decimal value 1.0, converting it to double
(with implied rounding), and then converting it to float (with a
second implied rounding).

If you write

float a = 1.0f;

you are taking the exact decimal value 1.0 and converting it directly
to float, with only one rounding operation.

Rounding once is never less accurate than rounding twice, and might
be more accurate.


My understanding is that there is no rounding needed for 1.0 (or for any
integer that involves less than the number of significant digits that a
floating point size supports). Is my understanding flawed, or are you really
making a point about numbers like, say, 1.2 which involve real rounding?

--
John Carson

Sep 19 '05 #5
"John Carson" <jc****************@netspace.net.au> wrote in message
news:dg***********@otis.netspace.net.au...
My understanding is that there is no rounding needed for 1.0 (or for any
integer that involves less than the number of significant digits that a
floating point size supports). Is my understanding flawed, or are you
really
making a point about numbers like, say, 1.2 which involve real rounding?


I am really making a point about numbers such as 1.2, which involve real
rounding.
Sep 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: maxim vexler | last post by:
in a book i am ready now : O'Reilly - Web Database Application with PHP and MySQL, 2nd ed. by David Lane, Hugh E. Williams on chapter 9 the author give an example for age validation :...
5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
5
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X...
4
by: Donald Newcomb | last post by:
I'm a real Python NEWB and am intrigued by some of Python's features, so I'm starting to write code to do some things to see how it works. So far I really like the lists and dictionaries since I...
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.