Why does auto deduce this variable as double and not float? [duplicate]why sizeof(13.33) is 8 bytes?Why...
90s(?) book series about two people transported to a parallel medieval world, she joins city watch, he becomes wizard
Why should someone be willing to write a strong recommendation even if that means losing a undergraduate from their lab?
insert several equation in one frame in beamer
Nuclear decay triggers
Does git delete empty folders?
Are objects subject to critical hits?
Vacuum collapse -- why do strong metals implode but glass doesn't?
Have only girls been born for a long time in this village?
Gofer work in exchange for Letter of Recommendation
Is there any road between the CA State Route 120 and Sherman Pass Road (Forest Route 22S0) that crosses Yosemite/Serria/Sequoia National Park/Forest?
Did the twin engined Lazair ultralight have a throttle for each engine?
Why the color Red in Us, what is the significance?
Does the Green Flame-Blade cantrip work with the Zephyr Strike spell?
How can I train a replacement without letting my bosses and the replacement know?
Earliest evidence of objects intended for future archaeologists?
In xXx, is Xander Cage's 10th vehicle a specific reference to another franchise?
The Lucky House
Starships without computers?
Sous vide chicken without an internal tempature of 165
Do living authors still get paid royalties for their old work?
Story that includes a description: "Two concentric circles, intersecting at three points"
Why do some academic journals requires a separate "summary" paragraph in addition to an abstract?
How to edit module/extensions code directly from Magento 2 admin dashboard as we can edit plugins in wordpress?
How to plot a Histogram and compare with a particular probability density function PDF
Why does auto deduce this variable as double and not float? [duplicate]
why sizeof(13.33) is 8 bytes?Why floating point value such as 3.14 are considered as double by default in MSVC?All floats are doubles?What is the type of the value 1.0e+1How a floating point literal is treated either double or float in Visual C++?How function overloading works with double and floatWhat is the most effective way for float and double comparison?Why can't variables be declared in a switch statement?Difference between decimal, float and double in .NET?What's the difference between a single precision and double precision floating point operation?How can I force division to be floating point? Division keeps rounding down to 0?What is the difference between float and double?Why not use Double or Float to represent currency?Should we generally use float literals for floats instead of the simpler double literals?Why does changing 0.1f to 0 slow down performance by 10x?Make C floating point literals float (rather than double)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
This question already has an answer here:
Why floating point value such as 3.14 are considered as double by default in MSVC?
5 answers
All floats are doubles?
3 answers
How a floating point literal is treated either double or float in Visual C++?
2 answers
why sizeof(13.33) is 8 bytes?
5 answers
What is the type of the value 1.0e+1
4 answers
In the snippet below, auto
deduces the variable to double
, but I want float
.
auto one = 3.5;
Does it always use double
for literals with a decimal point? How does it decide between float and double?
c++ floating-point literals auto type-deduction
marked as duplicate by phuclv, user207421
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
Why floating point value such as 3.14 are considered as double by default in MSVC?
5 answers
All floats are doubles?
3 answers
How a floating point literal is treated either double or float in Visual C++?
2 answers
why sizeof(13.33) is 8 bytes?
5 answers
What is the type of the value 1.0e+1
4 answers
In the snippet below, auto
deduces the variable to double
, but I want float
.
auto one = 3.5;
Does it always use double
for literals with a decimal point? How does it decide between float and double?
c++ floating-point literals auto type-deduction
marked as duplicate by phuclv, user207421
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
To answer your exact question (instead of what you wanted to know) --auto
deduces it that way because templates deduce it that way. Templates deduce it that way because function overloading does (function overloading rules are used to break ties).
– Ben Voigt
Aug 16 at 5:36
2
I mean to say that the rule that makesauto one = 3.5;
deducedouble
is the same rule that makeint f(float); int f(double);
choosef(double)
when presented withf(3.5)
. Your question has nothing peculiar toauto
, it only involves the type and category of the floating-point literal3.5
– Ben Voigt
Aug 16 at 5:43
8
Because3.5
is adouble
, not afloat
.
– user207421
Aug 16 at 6:11
1
@JustinTime: Although in C, character literals are typeint
, which can cause subtle errors when code that looks like C is compiled with both C and C++ compilers, but depends on character literals being specificallychar
orint
(and even more fun, potentially depends on the implementation defined signedness ofchar
).
– ShadowRanger
2 days ago
1
there are so many dupllicates: All floats are doubles?, How a floating point literal is treated either double or float in Visual C++?, why sizeof(13.33) is 8 bytes?, What is the type of the value 1.0e+1, How function overloading works with double and float...
– phuclv
2 days ago
|
show 2 more comments
This question already has an answer here:
Why floating point value such as 3.14 are considered as double by default in MSVC?
5 answers
All floats are doubles?
3 answers
How a floating point literal is treated either double or float in Visual C++?
2 answers
why sizeof(13.33) is 8 bytes?
5 answers
What is the type of the value 1.0e+1
4 answers
In the snippet below, auto
deduces the variable to double
, but I want float
.
auto one = 3.5;
Does it always use double
for literals with a decimal point? How does it decide between float and double?
c++ floating-point literals auto type-deduction
This question already has an answer here:
Why floating point value such as 3.14 are considered as double by default in MSVC?
5 answers
All floats are doubles?
3 answers
How a floating point literal is treated either double or float in Visual C++?
2 answers
why sizeof(13.33) is 8 bytes?
5 answers
What is the type of the value 1.0e+1
4 answers
In the snippet below, auto
deduces the variable to double
, but I want float
.
auto one = 3.5;
Does it always use double
for literals with a decimal point? How does it decide between float and double?
This question already has an answer here:
Why floating point value such as 3.14 are considered as double by default in MSVC?
5 answers
All floats are doubles?
3 answers
How a floating point literal is treated either double or float in Visual C++?
2 answers
why sizeof(13.33) is 8 bytes?
5 answers
What is the type of the value 1.0e+1
4 answers
c++ floating-point literals auto type-deduction
c++ floating-point literals auto type-deduction
edited 2 days ago
curiousguy
5,0382 gold badges30 silver badges46 bronze badges
5,0382 gold badges30 silver badges46 bronze badges
asked Aug 16 at 5:32
Abhinav KinagiAbhinav Kinagi
7484 silver badges22 bronze badges
7484 silver badges22 bronze badges
marked as duplicate by phuclv, user207421
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by phuclv, user207421
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by phuclv, user207421
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
To answer your exact question (instead of what you wanted to know) --auto
deduces it that way because templates deduce it that way. Templates deduce it that way because function overloading does (function overloading rules are used to break ties).
– Ben Voigt
Aug 16 at 5:36
2
I mean to say that the rule that makesauto one = 3.5;
deducedouble
is the same rule that makeint f(float); int f(double);
choosef(double)
when presented withf(3.5)
. Your question has nothing peculiar toauto
, it only involves the type and category of the floating-point literal3.5
– Ben Voigt
Aug 16 at 5:43
8
Because3.5
is adouble
, not afloat
.
– user207421
Aug 16 at 6:11
1
@JustinTime: Although in C, character literals are typeint
, which can cause subtle errors when code that looks like C is compiled with both C and C++ compilers, but depends on character literals being specificallychar
orint
(and even more fun, potentially depends on the implementation defined signedness ofchar
).
– ShadowRanger
2 days ago
1
there are so many dupllicates: All floats are doubles?, How a floating point literal is treated either double or float in Visual C++?, why sizeof(13.33) is 8 bytes?, What is the type of the value 1.0e+1, How function overloading works with double and float...
– phuclv
2 days ago
|
show 2 more comments
2
To answer your exact question (instead of what you wanted to know) --auto
deduces it that way because templates deduce it that way. Templates deduce it that way because function overloading does (function overloading rules are used to break ties).
– Ben Voigt
Aug 16 at 5:36
2
I mean to say that the rule that makesauto one = 3.5;
deducedouble
is the same rule that makeint f(float); int f(double);
choosef(double)
when presented withf(3.5)
. Your question has nothing peculiar toauto
, it only involves the type and category of the floating-point literal3.5
– Ben Voigt
Aug 16 at 5:43
8
Because3.5
is adouble
, not afloat
.
– user207421
Aug 16 at 6:11
1
@JustinTime: Although in C, character literals are typeint
, which can cause subtle errors when code that looks like C is compiled with both C and C++ compilers, but depends on character literals being specificallychar
orint
(and even more fun, potentially depends on the implementation defined signedness ofchar
).
– ShadowRanger
2 days ago
1
there are so many dupllicates: All floats are doubles?, How a floating point literal is treated either double or float in Visual C++?, why sizeof(13.33) is 8 bytes?, What is the type of the value 1.0e+1, How function overloading works with double and float...
– phuclv
2 days ago
2
2
To answer your exact question (instead of what you wanted to know) --
auto
deduces it that way because templates deduce it that way. Templates deduce it that way because function overloading does (function overloading rules are used to break ties).– Ben Voigt
Aug 16 at 5:36
To answer your exact question (instead of what you wanted to know) --
auto
deduces it that way because templates deduce it that way. Templates deduce it that way because function overloading does (function overloading rules are used to break ties).– Ben Voigt
Aug 16 at 5:36
2
2
I mean to say that the rule that makes
auto one = 3.5;
deduce double
is the same rule that make int f(float); int f(double);
choose f(double)
when presented with f(3.5)
. Your question has nothing peculiar to auto
, it only involves the type and category of the floating-point literal 3.5
– Ben Voigt
Aug 16 at 5:43
I mean to say that the rule that makes
auto one = 3.5;
deduce double
is the same rule that make int f(float); int f(double);
choose f(double)
when presented with f(3.5)
. Your question has nothing peculiar to auto
, it only involves the type and category of the floating-point literal 3.5
– Ben Voigt
Aug 16 at 5:43
8
8
Because
3.5
is a double
, not a float
.– user207421
Aug 16 at 6:11
Because
3.5
is a double
, not a float
.– user207421
Aug 16 at 6:11
1
1
@JustinTime: Although in C, character literals are type
int
, which can cause subtle errors when code that looks like C is compiled with both C and C++ compilers, but depends on character literals being specifically char
or int
(and even more fun, potentially depends on the implementation defined signedness of char
).– ShadowRanger
2 days ago
@JustinTime: Although in C, character literals are type
int
, which can cause subtle errors when code that looks like C is compiled with both C and C++ compilers, but depends on character literals being specifically char
or int
(and even more fun, potentially depends on the implementation defined signedness of char
).– ShadowRanger
2 days ago
1
1
there are so many dupllicates: All floats are doubles?, How a floating point literal is treated either double or float in Visual C++?, why sizeof(13.33) is 8 bytes?, What is the type of the value 1.0e+1, How function overloading works with double and float...
– phuclv
2 days ago
there are so many dupllicates: All floats are doubles?, How a floating point literal is treated either double or float in Visual C++?, why sizeof(13.33) is 8 bytes?, What is the type of the value 1.0e+1, How function overloading works with double and float...
– phuclv
2 days ago
|
show 2 more comments
4 Answers
4
active
oldest
votes
Type of literal 3.5
is double
. For float
please use 3.5f
You can play with this snippet to see various type information.
add a comment |
3.5
is a double
literal. Thus auto
correctly deduces its type as double
. You can still use it to initialize a float
variable, but the most correct way is to use a float
literal like 3.5f
. The f
at the end is called a suffix. Suffixes for floating point literals are:
- (no suffix) defines double
f
F
defines float
l
L
defines long double
Besides floating point literals, there are also suffixes for integral literals and user-defined literals.
add a comment |
In C++ (and C), floating literals are treated as double
by default unless specified by f or F or l or L
.
The standard has following:
2.14.4
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify
float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.
Hence,
auto one = 3.5;
is always double
and if you intend float
it should be coded as
auto one = 3.5f;
add a comment |
The type of a floating point literal in C++ is automatically double
unless:
f
is suffixed, in which case the type of the literal isfloat
L
is suffixed, in which case the type of the literal islong double
So, if you want your variable to be a float
, do this:
auto one = 3.5f;
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Type of literal 3.5
is double
. For float
please use 3.5f
You can play with this snippet to see various type information.
add a comment |
Type of literal 3.5
is double
. For float
please use 3.5f
You can play with this snippet to see various type information.
add a comment |
Type of literal 3.5
is double
. For float
please use 3.5f
You can play with this snippet to see various type information.
Type of literal 3.5
is double
. For float
please use 3.5f
You can play with this snippet to see various type information.
edited Aug 16 at 5:39
answered Aug 16 at 5:34
Gyapti JainGyapti Jain
3,52515 silver badges38 bronze badges
3,52515 silver badges38 bronze badges
add a comment |
add a comment |
3.5
is a double
literal. Thus auto
correctly deduces its type as double
. You can still use it to initialize a float
variable, but the most correct way is to use a float
literal like 3.5f
. The f
at the end is called a suffix. Suffixes for floating point literals are:
- (no suffix) defines double
f
F
defines float
l
L
defines long double
Besides floating point literals, there are also suffixes for integral literals and user-defined literals.
add a comment |
3.5
is a double
literal. Thus auto
correctly deduces its type as double
. You can still use it to initialize a float
variable, but the most correct way is to use a float
literal like 3.5f
. The f
at the end is called a suffix. Suffixes for floating point literals are:
- (no suffix) defines double
f
F
defines float
l
L
defines long double
Besides floating point literals, there are also suffixes for integral literals and user-defined literals.
add a comment |
3.5
is a double
literal. Thus auto
correctly deduces its type as double
. You can still use it to initialize a float
variable, but the most correct way is to use a float
literal like 3.5f
. The f
at the end is called a suffix. Suffixes for floating point literals are:
- (no suffix) defines double
f
F
defines float
l
L
defines long double
Besides floating point literals, there are also suffixes for integral literals and user-defined literals.
3.5
is a double
literal. Thus auto
correctly deduces its type as double
. You can still use it to initialize a float
variable, but the most correct way is to use a float
literal like 3.5f
. The f
at the end is called a suffix. Suffixes for floating point literals are:
- (no suffix) defines double
f
F
defines float
l
L
defines long double
Besides floating point literals, there are also suffixes for integral literals and user-defined literals.
answered Aug 16 at 6:03
AyxanAyxan
5,0361 gold badge11 silver badges36 bronze badges
5,0361 gold badge11 silver badges36 bronze badges
add a comment |
add a comment |
In C++ (and C), floating literals are treated as double
by default unless specified by f or F or l or L
.
The standard has following:
2.14.4
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify
float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.
Hence,
auto one = 3.5;
is always double
and if you intend float
it should be coded as
auto one = 3.5f;
add a comment |
In C++ (and C), floating literals are treated as double
by default unless specified by f or F or l or L
.
The standard has following:
2.14.4
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify
float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.
Hence,
auto one = 3.5;
is always double
and if you intend float
it should be coded as
auto one = 3.5f;
add a comment |
In C++ (and C), floating literals are treated as double
by default unless specified by f or F or l or L
.
The standard has following:
2.14.4
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify
float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.
Hence,
auto one = 3.5;
is always double
and if you intend float
it should be coded as
auto one = 3.5f;
In C++ (and C), floating literals are treated as double
by default unless specified by f or F or l or L
.
The standard has following:
2.14.4
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify
float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.
Hence,
auto one = 3.5;
is always double
and if you intend float
it should be coded as
auto one = 3.5f;
edited Aug 16 at 7:09
user207421
269k28 gold badges227 silver badges381 bronze badges
269k28 gold badges227 silver badges381 bronze badges
answered Aug 16 at 6:29
dlmeeteidlmeetei
4,6581 gold badge18 silver badges30 bronze badges
4,6581 gold badge18 silver badges30 bronze badges
add a comment |
add a comment |
The type of a floating point literal in C++ is automatically double
unless:
f
is suffixed, in which case the type of the literal isfloat
L
is suffixed, in which case the type of the literal islong double
So, if you want your variable to be a float
, do this:
auto one = 3.5f;
add a comment |
The type of a floating point literal in C++ is automatically double
unless:
f
is suffixed, in which case the type of the literal isfloat
L
is suffixed, in which case the type of the literal islong double
So, if you want your variable to be a float
, do this:
auto one = 3.5f;
add a comment |
The type of a floating point literal in C++ is automatically double
unless:
f
is suffixed, in which case the type of the literal isfloat
L
is suffixed, in which case the type of the literal islong double
So, if you want your variable to be a float
, do this:
auto one = 3.5f;
The type of a floating point literal in C++ is automatically double
unless:
f
is suffixed, in which case the type of the literal isfloat
L
is suffixed, in which case the type of the literal islong double
So, if you want your variable to be a float
, do this:
auto one = 3.5f;
answered 2 days ago
JL2210JL2210
2,9262 gold badges11 silver badges35 bronze badges
2,9262 gold badges11 silver badges35 bronze badges
add a comment |
add a comment |
2
To answer your exact question (instead of what you wanted to know) --
auto
deduces it that way because templates deduce it that way. Templates deduce it that way because function overloading does (function overloading rules are used to break ties).– Ben Voigt
Aug 16 at 5:36
2
I mean to say that the rule that makes
auto one = 3.5;
deducedouble
is the same rule that makeint f(float); int f(double);
choosef(double)
when presented withf(3.5)
. Your question has nothing peculiar toauto
, it only involves the type and category of the floating-point literal3.5
– Ben Voigt
Aug 16 at 5:43
8
Because
3.5
is adouble
, not afloat
.– user207421
Aug 16 at 6:11
1
@JustinTime: Although in C, character literals are type
int
, which can cause subtle errors when code that looks like C is compiled with both C and C++ compilers, but depends on character literals being specificallychar
orint
(and even more fun, potentially depends on the implementation defined signedness ofchar
).– ShadowRanger
2 days ago
1
there are so many dupllicates: All floats are doubles?, How a floating point literal is treated either double or float in Visual C++?, why sizeof(13.33) is 8 bytes?, What is the type of the value 1.0e+1, How function overloading works with double and float...
– phuclv
2 days ago