How can I fix my floats being rounded down to doubles?










0















I know that by default in C when you declare a float it gets automatically saved as a double and that if you want it to be saved as a float you have to declare it like this



float x = 0.11f


but what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?
Here's my code btw, thanks for the help.



#include <stdio.h>

int main()
float number = 0;
float comparison;

do
printf("nEnter a number: ");
scanf("%f", &comparison);

if(comparison > number)
number = comparison;

while(comparison > 0);

printf("The largest number enteres was: %fnn", number);











share|improve this question
























  • Store the float as two integers, as integer and fractional part or as integer part and exponent. You may interest yourself in Is floating point math broken?.

    – Kamil Cuk
    Nov 13 '18 at 13:55







  • 4





    Scanf with %f reads float. With %lf it reads a double. Also, double has higher precision and range than float so it doesn't make sense to talk about floats being "rounded down" to doubles.

    – interjay
    Nov 13 '18 at 13:56






  • 2





    By the way, number is uninitialized in your code.

    – interjay
    Nov 13 '18 at 13:56











  • @KamilCuk This works but you need special functions as soon as you start computing them.

    – Jean-Marc Zimmer
    Nov 13 '18 at 13:56






  • 1





    Neither 60.4591 nor 99.2 can be represented exactly with IEEE 754 floats. There is no rounding involved. Make sure to read the answers to the question linked in the very first comment.

    – Swordfish
    Nov 13 '18 at 14:02















0















I know that by default in C when you declare a float it gets automatically saved as a double and that if you want it to be saved as a float you have to declare it like this



float x = 0.11f


but what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?
Here's my code btw, thanks for the help.



#include <stdio.h>

int main()
float number = 0;
float comparison;

do
printf("nEnter a number: ");
scanf("%f", &comparison);

if(comparison > number)
number = comparison;

while(comparison > 0);

printf("The largest number enteres was: %fnn", number);











share|improve this question
























  • Store the float as two integers, as integer and fractional part or as integer part and exponent. You may interest yourself in Is floating point math broken?.

    – Kamil Cuk
    Nov 13 '18 at 13:55







  • 4





    Scanf with %f reads float. With %lf it reads a double. Also, double has higher precision and range than float so it doesn't make sense to talk about floats being "rounded down" to doubles.

    – interjay
    Nov 13 '18 at 13:56






  • 2





    By the way, number is uninitialized in your code.

    – interjay
    Nov 13 '18 at 13:56











  • @KamilCuk This works but you need special functions as soon as you start computing them.

    – Jean-Marc Zimmer
    Nov 13 '18 at 13:56






  • 1





    Neither 60.4591 nor 99.2 can be represented exactly with IEEE 754 floats. There is no rounding involved. Make sure to read the answers to the question linked in the very first comment.

    – Swordfish
    Nov 13 '18 at 14:02













0












0








0








I know that by default in C when you declare a float it gets automatically saved as a double and that if you want it to be saved as a float you have to declare it like this



float x = 0.11f


but what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?
Here's my code btw, thanks for the help.



#include <stdio.h>

int main()
float number = 0;
float comparison;

do
printf("nEnter a number: ");
scanf("%f", &comparison);

if(comparison > number)
number = comparison;

while(comparison > 0);

printf("The largest number enteres was: %fnn", number);











share|improve this question
















I know that by default in C when you declare a float it gets automatically saved as a double and that if you want it to be saved as a float you have to declare it like this



float x = 0.11f


but what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?
Here's my code btw, thanks for the help.



#include <stdio.h>

int main()
float number = 0;
float comparison;

do
printf("nEnter a number: ");
scanf("%f", &comparison);

if(comparison > number)
number = comparison;

while(comparison > 0);

printf("The largest number enteres was: %fnn", number);








c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:02







FoxyIT

















asked Nov 13 '18 at 13:52









FoxyITFoxyIT

907




907












  • Store the float as two integers, as integer and fractional part or as integer part and exponent. You may interest yourself in Is floating point math broken?.

    – Kamil Cuk
    Nov 13 '18 at 13:55







  • 4





    Scanf with %f reads float. With %lf it reads a double. Also, double has higher precision and range than float so it doesn't make sense to talk about floats being "rounded down" to doubles.

    – interjay
    Nov 13 '18 at 13:56






  • 2





    By the way, number is uninitialized in your code.

    – interjay
    Nov 13 '18 at 13:56











  • @KamilCuk This works but you need special functions as soon as you start computing them.

    – Jean-Marc Zimmer
    Nov 13 '18 at 13:56






  • 1





    Neither 60.4591 nor 99.2 can be represented exactly with IEEE 754 floats. There is no rounding involved. Make sure to read the answers to the question linked in the very first comment.

    – Swordfish
    Nov 13 '18 at 14:02

















  • Store the float as two integers, as integer and fractional part or as integer part and exponent. You may interest yourself in Is floating point math broken?.

    – Kamil Cuk
    Nov 13 '18 at 13:55







  • 4





    Scanf with %f reads float. With %lf it reads a double. Also, double has higher precision and range than float so it doesn't make sense to talk about floats being "rounded down" to doubles.

    – interjay
    Nov 13 '18 at 13:56






  • 2





    By the way, number is uninitialized in your code.

    – interjay
    Nov 13 '18 at 13:56











  • @KamilCuk This works but you need special functions as soon as you start computing them.

    – Jean-Marc Zimmer
    Nov 13 '18 at 13:56






  • 1





    Neither 60.4591 nor 99.2 can be represented exactly with IEEE 754 floats. There is no rounding involved. Make sure to read the answers to the question linked in the very first comment.

    – Swordfish
    Nov 13 '18 at 14:02
















Store the float as two integers, as integer and fractional part or as integer part and exponent. You may interest yourself in Is floating point math broken?.

– Kamil Cuk
Nov 13 '18 at 13:55






Store the float as two integers, as integer and fractional part or as integer part and exponent. You may interest yourself in Is floating point math broken?.

– Kamil Cuk
Nov 13 '18 at 13:55





4




4





Scanf with %f reads float. With %lf it reads a double. Also, double has higher precision and range than float so it doesn't make sense to talk about floats being "rounded down" to doubles.

– interjay
Nov 13 '18 at 13:56





Scanf with %f reads float. With %lf it reads a double. Also, double has higher precision and range than float so it doesn't make sense to talk about floats being "rounded down" to doubles.

– interjay
Nov 13 '18 at 13:56




2




2





By the way, number is uninitialized in your code.

– interjay
Nov 13 '18 at 13:56





By the way, number is uninitialized in your code.

– interjay
Nov 13 '18 at 13:56













@KamilCuk This works but you need special functions as soon as you start computing them.

– Jean-Marc Zimmer
Nov 13 '18 at 13:56





@KamilCuk This works but you need special functions as soon as you start computing them.

– Jean-Marc Zimmer
Nov 13 '18 at 13:56




1




1





Neither 60.4591 nor 99.2 can be represented exactly with IEEE 754 floats. There is no rounding involved. Make sure to read the answers to the question linked in the very first comment.

– Swordfish
Nov 13 '18 at 14:02





Neither 60.4591 nor 99.2 can be represented exactly with IEEE 754 floats. There is no rounding involved. Make sure to read the answers to the question linked in the very first comment.

– Swordfish
Nov 13 '18 at 14:02












1 Answer
1






active

oldest

votes


















1















what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?




scanf with an %f directive will read the input and convert it to a float (not a double). If the matched text does not correspond to a number exactly representable as a float then there will be rounding at this stage. There is no alternative.



When you pass an argument of type float to printf() for printing, it will be promoted to type double. This is required by the signature of that function. But type double can exactly represent all values of type float, so this promotion does not involve any rounding. printf's handling of the %f directives is aligned with this automatic promotion: the corresponding (promoted) argument is expected to be of type double.



There are multiple avenues to reproducing the input exactly, depending on what constraints you are willing to put on that input. The most general is to read, store, and print the data as a string, though even this has its complications.



If you are willing to place a limit on the maximum decimal range and precision for which verbatim reproduction is supported, then you may be able to get output rounded to the same representation as the input by specifying a precision in your printf field directives:



float f;
scanf("%f", &f);
printf("%f %.2f %5.2fn", f, f, f);


If you want to use a built-in floating-point format and also avoid trailing zeroes being appended then either an explicit precision like that or a %g directive is probably needed:



printf("%f %gn", f, f);


Other alternatives are more involved, such as creating a fixed-point or arbitrary-precision decimal data type, along with appropriate functions for reading and writing it. I presume that goes beyond what you're presently interested in doing.




Note: "double" is short for "double precision", as opposed to notionally single-precision "float". The former is the larger type in terms of storage and representational capability. In real-world implementations, there is never any "rounding down" from float to double.






share|improve this answer























  • Thanks for the further clarification! Also the g format worked perfectly. One more question: if the printf automatically promotes all float to doubles once it prints them out, isn't there a way to cast the variable into a float before it gets printed out? Also, is it correct to say that "float a = 4.21" is treated as a double by the computer while "float a = 4.21f" is treated as a float?

    – FoxyIT
    Nov 13 '18 at 17:39











  • @FoxyIT, your variable a is a float, regardless of the type of the value with which you initialize it, or the type of any value you later assign to it. It stores floats and only floats, and conversion to type float upon initialization or assignment is automatic. But when you specify that variable as an argument to printf(), the value read from it is promoted to type double for delivery to the function. The same would happen to the result of a redundant cast. But that conversion is value-preserving; I don't understand why you're fixating on it.

    – John Bollinger
    Nov 13 '18 at 18:00












  • Then why is it better to specify "f" at the end of the value of a float? What's its purpose?

    – FoxyIT
    Nov 13 '18 at 18:37






  • 1





    @FoxyIT, a floating-point constant ending in f or F has type float, whereas one without a suffix has type double. The only reason to prefer the former for initializing a variable of type float is to silence compiler warnings about loss of precision. But using float literals instead in such contexts does not improve precision; it just demonstrates to the compiler's satisfaction that you really do want the lesser precision of a float rather than the greater precision of a double.

    – John Bollinger
    Nov 13 '18 at 18:51











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282550%2fhow-can-i-fix-my-floats-being-rounded-down-to-doubles%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?




scanf with an %f directive will read the input and convert it to a float (not a double). If the matched text does not correspond to a number exactly representable as a float then there will be rounding at this stage. There is no alternative.



When you pass an argument of type float to printf() for printing, it will be promoted to type double. This is required by the signature of that function. But type double can exactly represent all values of type float, so this promotion does not involve any rounding. printf's handling of the %f directives is aligned with this automatic promotion: the corresponding (promoted) argument is expected to be of type double.



There are multiple avenues to reproducing the input exactly, depending on what constraints you are willing to put on that input. The most general is to read, store, and print the data as a string, though even this has its complications.



If you are willing to place a limit on the maximum decimal range and precision for which verbatim reproduction is supported, then you may be able to get output rounded to the same representation as the input by specifying a precision in your printf field directives:



float f;
scanf("%f", &f);
printf("%f %.2f %5.2fn", f, f, f);


If you want to use a built-in floating-point format and also avoid trailing zeroes being appended then either an explicit precision like that or a %g directive is probably needed:



printf("%f %gn", f, f);


Other alternatives are more involved, such as creating a fixed-point or arbitrary-precision decimal data type, along with appropriate functions for reading and writing it. I presume that goes beyond what you're presently interested in doing.




Note: "double" is short for "double precision", as opposed to notionally single-precision "float". The former is the larger type in terms of storage and representational capability. In real-world implementations, there is never any "rounding down" from float to double.






share|improve this answer























  • Thanks for the further clarification! Also the g format worked perfectly. One more question: if the printf automatically promotes all float to doubles once it prints them out, isn't there a way to cast the variable into a float before it gets printed out? Also, is it correct to say that "float a = 4.21" is treated as a double by the computer while "float a = 4.21f" is treated as a float?

    – FoxyIT
    Nov 13 '18 at 17:39











  • @FoxyIT, your variable a is a float, regardless of the type of the value with which you initialize it, or the type of any value you later assign to it. It stores floats and only floats, and conversion to type float upon initialization or assignment is automatic. But when you specify that variable as an argument to printf(), the value read from it is promoted to type double for delivery to the function. The same would happen to the result of a redundant cast. But that conversion is value-preserving; I don't understand why you're fixating on it.

    – John Bollinger
    Nov 13 '18 at 18:00












  • Then why is it better to specify "f" at the end of the value of a float? What's its purpose?

    – FoxyIT
    Nov 13 '18 at 18:37






  • 1





    @FoxyIT, a floating-point constant ending in f or F has type float, whereas one without a suffix has type double. The only reason to prefer the former for initializing a variable of type float is to silence compiler warnings about loss of precision. But using float literals instead in such contexts does not improve precision; it just demonstrates to the compiler's satisfaction that you really do want the lesser precision of a float rather than the greater precision of a double.

    – John Bollinger
    Nov 13 '18 at 18:51
















1















what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?




scanf with an %f directive will read the input and convert it to a float (not a double). If the matched text does not correspond to a number exactly representable as a float then there will be rounding at this stage. There is no alternative.



When you pass an argument of type float to printf() for printing, it will be promoted to type double. This is required by the signature of that function. But type double can exactly represent all values of type float, so this promotion does not involve any rounding. printf's handling of the %f directives is aligned with this automatic promotion: the corresponding (promoted) argument is expected to be of type double.



There are multiple avenues to reproducing the input exactly, depending on what constraints you are willing to put on that input. The most general is to read, store, and print the data as a string, though even this has its complications.



If you are willing to place a limit on the maximum decimal range and precision for which verbatim reproduction is supported, then you may be able to get output rounded to the same representation as the input by specifying a precision in your printf field directives:



float f;
scanf("%f", &f);
printf("%f %.2f %5.2fn", f, f, f);


If you want to use a built-in floating-point format and also avoid trailing zeroes being appended then either an explicit precision like that or a %g directive is probably needed:



printf("%f %gn", f, f);


Other alternatives are more involved, such as creating a fixed-point or arbitrary-precision decimal data type, along with appropriate functions for reading and writing it. I presume that goes beyond what you're presently interested in doing.




Note: "double" is short for "double precision", as opposed to notionally single-precision "float". The former is the larger type in terms of storage and representational capability. In real-world implementations, there is never any "rounding down" from float to double.






share|improve this answer























  • Thanks for the further clarification! Also the g format worked perfectly. One more question: if the printf automatically promotes all float to doubles once it prints them out, isn't there a way to cast the variable into a float before it gets printed out? Also, is it correct to say that "float a = 4.21" is treated as a double by the computer while "float a = 4.21f" is treated as a float?

    – FoxyIT
    Nov 13 '18 at 17:39











  • @FoxyIT, your variable a is a float, regardless of the type of the value with which you initialize it, or the type of any value you later assign to it. It stores floats and only floats, and conversion to type float upon initialization or assignment is automatic. But when you specify that variable as an argument to printf(), the value read from it is promoted to type double for delivery to the function. The same would happen to the result of a redundant cast. But that conversion is value-preserving; I don't understand why you're fixating on it.

    – John Bollinger
    Nov 13 '18 at 18:00












  • Then why is it better to specify "f" at the end of the value of a float? What's its purpose?

    – FoxyIT
    Nov 13 '18 at 18:37






  • 1





    @FoxyIT, a floating-point constant ending in f or F has type float, whereas one without a suffix has type double. The only reason to prefer the former for initializing a variable of type float is to silence compiler warnings about loss of precision. But using float literals instead in such contexts does not improve precision; it just demonstrates to the compiler's satisfaction that you really do want the lesser precision of a float rather than the greater precision of a double.

    – John Bollinger
    Nov 13 '18 at 18:51














1












1








1








what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?




scanf with an %f directive will read the input and convert it to a float (not a double). If the matched text does not correspond to a number exactly representable as a float then there will be rounding at this stage. There is no alternative.



When you pass an argument of type float to printf() for printing, it will be promoted to type double. This is required by the signature of that function. But type double can exactly represent all values of type float, so this promotion does not involve any rounding. printf's handling of the %f directives is aligned with this automatic promotion: the corresponding (promoted) argument is expected to be of type double.



There are multiple avenues to reproducing the input exactly, depending on what constraints you are willing to put on that input. The most general is to read, store, and print the data as a string, though even this has its complications.



If you are willing to place a limit on the maximum decimal range and precision for which verbatim reproduction is supported, then you may be able to get output rounded to the same representation as the input by specifying a precision in your printf field directives:



float f;
scanf("%f", &f);
printf("%f %.2f %5.2fn", f, f, f);


If you want to use a built-in floating-point format and also avoid trailing zeroes being appended then either an explicit precision like that or a %g directive is probably needed:



printf("%f %gn", f, f);


Other alternatives are more involved, such as creating a fixed-point or arbitrary-precision decimal data type, along with appropriate functions for reading and writing it. I presume that goes beyond what you're presently interested in doing.




Note: "double" is short for "double precision", as opposed to notionally single-precision "float". The former is the larger type in terms of storage and representational capability. In real-world implementations, there is never any "rounding down" from float to double.






share|improve this answer














what if my x value comes from a scanf? How can I do so that when I print it it doesn't get rounded down or up?




scanf with an %f directive will read the input and convert it to a float (not a double). If the matched text does not correspond to a number exactly representable as a float then there will be rounding at this stage. There is no alternative.



When you pass an argument of type float to printf() for printing, it will be promoted to type double. This is required by the signature of that function. But type double can exactly represent all values of type float, so this promotion does not involve any rounding. printf's handling of the %f directives is aligned with this automatic promotion: the corresponding (promoted) argument is expected to be of type double.



There are multiple avenues to reproducing the input exactly, depending on what constraints you are willing to put on that input. The most general is to read, store, and print the data as a string, though even this has its complications.



If you are willing to place a limit on the maximum decimal range and precision for which verbatim reproduction is supported, then you may be able to get output rounded to the same representation as the input by specifying a precision in your printf field directives:



float f;
scanf("%f", &f);
printf("%f %.2f %5.2fn", f, f, f);


If you want to use a built-in floating-point format and also avoid trailing zeroes being appended then either an explicit precision like that or a %g directive is probably needed:



printf("%f %gn", f, f);


Other alternatives are more involved, such as creating a fixed-point or arbitrary-precision decimal data type, along with appropriate functions for reading and writing it. I presume that goes beyond what you're presently interested in doing.




Note: "double" is short for "double precision", as opposed to notionally single-precision "float". The former is the larger type in terms of storage and representational capability. In real-world implementations, there is never any "rounding down" from float to double.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 15:18









John BollingerJohn Bollinger

81.9k74277




81.9k74277












  • Thanks for the further clarification! Also the g format worked perfectly. One more question: if the printf automatically promotes all float to doubles once it prints them out, isn't there a way to cast the variable into a float before it gets printed out? Also, is it correct to say that "float a = 4.21" is treated as a double by the computer while "float a = 4.21f" is treated as a float?

    – FoxyIT
    Nov 13 '18 at 17:39











  • @FoxyIT, your variable a is a float, regardless of the type of the value with which you initialize it, or the type of any value you later assign to it. It stores floats and only floats, and conversion to type float upon initialization or assignment is automatic. But when you specify that variable as an argument to printf(), the value read from it is promoted to type double for delivery to the function. The same would happen to the result of a redundant cast. But that conversion is value-preserving; I don't understand why you're fixating on it.

    – John Bollinger
    Nov 13 '18 at 18:00












  • Then why is it better to specify "f" at the end of the value of a float? What's its purpose?

    – FoxyIT
    Nov 13 '18 at 18:37






  • 1





    @FoxyIT, a floating-point constant ending in f or F has type float, whereas one without a suffix has type double. The only reason to prefer the former for initializing a variable of type float is to silence compiler warnings about loss of precision. But using float literals instead in such contexts does not improve precision; it just demonstrates to the compiler's satisfaction that you really do want the lesser precision of a float rather than the greater precision of a double.

    – John Bollinger
    Nov 13 '18 at 18:51


















  • Thanks for the further clarification! Also the g format worked perfectly. One more question: if the printf automatically promotes all float to doubles once it prints them out, isn't there a way to cast the variable into a float before it gets printed out? Also, is it correct to say that "float a = 4.21" is treated as a double by the computer while "float a = 4.21f" is treated as a float?

    – FoxyIT
    Nov 13 '18 at 17:39











  • @FoxyIT, your variable a is a float, regardless of the type of the value with which you initialize it, or the type of any value you later assign to it. It stores floats and only floats, and conversion to type float upon initialization or assignment is automatic. But when you specify that variable as an argument to printf(), the value read from it is promoted to type double for delivery to the function. The same would happen to the result of a redundant cast. But that conversion is value-preserving; I don't understand why you're fixating on it.

    – John Bollinger
    Nov 13 '18 at 18:00












  • Then why is it better to specify "f" at the end of the value of a float? What's its purpose?

    – FoxyIT
    Nov 13 '18 at 18:37






  • 1





    @FoxyIT, a floating-point constant ending in f or F has type float, whereas one without a suffix has type double. The only reason to prefer the former for initializing a variable of type float is to silence compiler warnings about loss of precision. But using float literals instead in such contexts does not improve precision; it just demonstrates to the compiler's satisfaction that you really do want the lesser precision of a float rather than the greater precision of a double.

    – John Bollinger
    Nov 13 '18 at 18:51

















Thanks for the further clarification! Also the g format worked perfectly. One more question: if the printf automatically promotes all float to doubles once it prints them out, isn't there a way to cast the variable into a float before it gets printed out? Also, is it correct to say that "float a = 4.21" is treated as a double by the computer while "float a = 4.21f" is treated as a float?

– FoxyIT
Nov 13 '18 at 17:39





Thanks for the further clarification! Also the g format worked perfectly. One more question: if the printf automatically promotes all float to doubles once it prints them out, isn't there a way to cast the variable into a float before it gets printed out? Also, is it correct to say that "float a = 4.21" is treated as a double by the computer while "float a = 4.21f" is treated as a float?

– FoxyIT
Nov 13 '18 at 17:39













@FoxyIT, your variable a is a float, regardless of the type of the value with which you initialize it, or the type of any value you later assign to it. It stores floats and only floats, and conversion to type float upon initialization or assignment is automatic. But when you specify that variable as an argument to printf(), the value read from it is promoted to type double for delivery to the function. The same would happen to the result of a redundant cast. But that conversion is value-preserving; I don't understand why you're fixating on it.

– John Bollinger
Nov 13 '18 at 18:00






@FoxyIT, your variable a is a float, regardless of the type of the value with which you initialize it, or the type of any value you later assign to it. It stores floats and only floats, and conversion to type float upon initialization or assignment is automatic. But when you specify that variable as an argument to printf(), the value read from it is promoted to type double for delivery to the function. The same would happen to the result of a redundant cast. But that conversion is value-preserving; I don't understand why you're fixating on it.

– John Bollinger
Nov 13 '18 at 18:00














Then why is it better to specify "f" at the end of the value of a float? What's its purpose?

– FoxyIT
Nov 13 '18 at 18:37





Then why is it better to specify "f" at the end of the value of a float? What's its purpose?

– FoxyIT
Nov 13 '18 at 18:37




1




1





@FoxyIT, a floating-point constant ending in f or F has type float, whereas one without a suffix has type double. The only reason to prefer the former for initializing a variable of type float is to silence compiler warnings about loss of precision. But using float literals instead in such contexts does not improve precision; it just demonstrates to the compiler's satisfaction that you really do want the lesser precision of a float rather than the greater precision of a double.

– John Bollinger
Nov 13 '18 at 18:51






@FoxyIT, a floating-point constant ending in f or F has type float, whereas one without a suffix has type double. The only reason to prefer the former for initializing a variable of type float is to silence compiler warnings about loss of precision. But using float literals instead in such contexts does not improve precision; it just demonstrates to the compiler's satisfaction that you really do want the lesser precision of a float rather than the greater precision of a double.

– John Bollinger
Nov 13 '18 at 18:51




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282550%2fhow-can-i-fix-my-floats-being-rounded-down-to-doubles%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo