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;
}







9
















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?










share|improve this question
















marked as duplicate by phuclv, user207421 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    Because 3.5 is a double, not a float.

    – 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 specifically char or int (and even more fun, potentially depends on the implementation defined signedness of char).

    – 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




















9
















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?










share|improve this question
















marked as duplicate by phuclv, user207421 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    Because 3.5 is a double, not a float.

    – 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 specifically char or int (and even more fun, potentially depends on the implementation defined signedness of char).

    – 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
















9












9








9









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?










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    Because 3.5 is a double, not a float.

    – 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 specifically char or int (and even more fun, potentially depends on the implementation defined signedness of char).

    – 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





    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; 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





    Because 3.5 is a double, not a float.

    – 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 specifically char or int (and even more fun, potentially depends on the implementation defined signedness of char).

    – 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














4 Answers
4






active

oldest

votes


















28














Type of literal 3.5 is double. For float please use 3.5f



You can play with this snippet to see various type information.






share|improve this answer



































    8














    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.






    share|improve this answer

































      6














      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;





      share|improve this answer



































        0














        The type of a floating point literal in C++ is automatically double unless:




        1. f is suffixed, in which case the type of the literal is float


        2. L is suffixed, in which case the type of the literal is long double



        So, if you want your variable to be a float, do this:



        auto one = 3.5f;





        share|improve this answer


































          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          28














          Type of literal 3.5 is double. For float please use 3.5f



          You can play with this snippet to see various type information.






          share|improve this answer
































            28














            Type of literal 3.5 is double. For float please use 3.5f



            You can play with this snippet to see various type information.






            share|improve this answer






























              28












              28








              28







              Type of literal 3.5 is double. For float please use 3.5f



              You can play with this snippet to see various type information.






              share|improve this answer















              Type of literal 3.5 is double. For float please use 3.5f



              You can play with this snippet to see various type information.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              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




























                  8














                  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.






                  share|improve this answer






























                    8














                    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.






                    share|improve this answer




























                      8












                      8








                      8







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 16 at 6:03









                      AyxanAyxan

                      5,0361 gold badge11 silver badges36 bronze badges




                      5,0361 gold badge11 silver badges36 bronze badges


























                          6














                          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;





                          share|improve this answer
































                            6














                            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;





                            share|improve this answer






























                              6












                              6








                              6







                              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;





                              share|improve this answer















                              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;






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              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


























                                  0














                                  The type of a floating point literal in C++ is automatically double unless:




                                  1. f is suffixed, in which case the type of the literal is float


                                  2. L is suffixed, in which case the type of the literal is long double



                                  So, if you want your variable to be a float, do this:



                                  auto one = 3.5f;





                                  share|improve this answer






























                                    0














                                    The type of a floating point literal in C++ is automatically double unless:




                                    1. f is suffixed, in which case the type of the literal is float


                                    2. L is suffixed, in which case the type of the literal is long double



                                    So, if you want your variable to be a float, do this:



                                    auto one = 3.5f;





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      The type of a floating point literal in C++ is automatically double unless:




                                      1. f is suffixed, in which case the type of the literal is float


                                      2. L is suffixed, in which case the type of the literal is long double



                                      So, if you want your variable to be a float, do this:



                                      auto one = 3.5f;





                                      share|improve this answer













                                      The type of a floating point literal in C++ is automatically double unless:




                                      1. f is suffixed, in which case the type of the literal is float


                                      2. L is suffixed, in which case the type of the literal is long double



                                      So, if you want your variable to be a float, do this:



                                      auto one = 3.5f;






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 2 days ago









                                      JL2210JL2210

                                      2,9262 gold badges11 silver badges35 bronze badges




                                      2,9262 gold badges11 silver badges35 bronze badges


















                                          Popular posts from this blog

                                          Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                                          Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                                          Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...