# Challenge output = 45.139999

#1

Hello,

First time posting! Thank you very much I am very new and very excited to get started with programming. I used the suggested float values on page 22 of 3.14 and 42.0 for my challenge. Here is my code. I’m just curious why I didn’t get an even 45.140000 value and received a 45.139999 value instead?

Thanks very much!

Also, \n simply means new line right? So it can actually be optional here but still best practice, correct?
-LS

int main (int argc, const char * argv[])
{
float varA;
float varB;
varA = 3.14;
varB = 42.0;
double total;
total = varA + varB;
printf(“The total of two floats is %f.\n”, total);

return 0;

}

#2

It’s because the computer is fundamentally unable to store every fraction exactly.

For instance, think of how you write 10 divided by 3: it’s 3.33333. But the 3 repeats forever. That mean decimals can not represent 1/3 exactly, because you don’t have an infinite supply of “3” to write out.

The computer is using base 2 instead of base 10, but the idea is the same. It’s a good point to remember when programming that floating point numbers are often close approximations.

Try adding these lines and see what happens:
printf(“The total of two floats is %.1f.\n”, total);
printf(“The total of two floats is %.2f.\n”, total);
printf(“The total of two floats is %.3f.\n”, total);

And you’re right about /n, it’s just another character that starts a new line.

#3

Thanks jameshaley,

I understand it much better now. I was able to use your three examples to truncate and and also learned it rounds the values off to even numbers such as 45.1, 45.14, 45.140. Great examples thanks very much.

-LS

#4

Could someone check if i did everything of the first challenge right?
Here is what i did:

float distance = 3.14;
printf("distance %f.\n", distance);

float time = 5.0;
printf("time %f.\n", time);

double speed;
speed = distance / time;
printf("speed %f.\n", speed);

#5

I am new this whole programming thing. I got the same results, but my code was slightly different. Any input on if this could have been written better would be greatly appreciated. Thanks. P.S. - Keep in mind that I am new, and kind of have to walk myself through these steps. lol

#include <stdio.h>

int main(int argc, const char * argv[])
{
//Declare first float type a variable
float varA;

//Give first float type a value
varA = 3.14;

//Declare second float type a variable
float varB;

//Give second float type a value
varB = 42.0;

//Declare a double type a variable
double sum;

//Give a double type a value
sum = varA + varB;

//Use printf to show results.
printf("The sum of varA and varB is %f.\n", sum);

//End function and indicate success.

return 0;

}

RESULTS - The sum of varA and varB is 45.139999.

#6

#7

Not bad code at all, the only suggestion I would make is that in the chapter the author mentions that “you can declare a variable and assign it an initial value in one line”. Thus you could slightly compact your code by writing this:

#include <stdio.h>

int main(int argc, const char * argv[])
{
//Declare first float type a variable and a value
float varA = 3.14;

//Declare second float type a variable and a value
float varB = 42.0;

//Declare a double type a variable and a value
double sum = varA + varB;

//Use printf to show results.
printf(“The sum of varA and varB is %f.\n”, sum);

//End function and indicate success.

return 0;
}

And of course you could alway add the suggestions made by jameshaley and substitute “%f” in the printf function for “%.2f” to give you nice number rounded to two decimal places.

#8

Nice to know about the %,2f thingamajig. Thanks.

#9

Thanks jameshaley, I was scratching my head on the result I was getting.

My Output is now showing the following after changing '%f" to ‘%.1f’, ‘%.2f’, and ‘%.3f’

First Number value is 3.140000.
Second Number value is 42.000000,
The Total is 45.139999.
The Total is 45.1.
The Total is 45.14.
The Total is 45.140.

[CODE]
{

// Declaring the first variable 'first' with assigned value of '3.14'
float first = 3.14;

// Declaring the second variable 'second' with assigned value of '42.0'
float second = 42.0;

// Adding two variables first and second
double result = first + second;

// Printing the result to log...
printf("First Number value is %f.\n", first);
printf("Second Number value is %f, \n", second);
printf("The Total is %f. \n", result);
printf("The Total is %.1f. \n", result);
printf("The Total is %.2f. \n", result);
printf("The Total is %.3f. \n", result);

return 0;

}

[Code]

#10

Hello,
My first day with the book and all fine so far.
Some good tips further up this post too.
I am trying to understand the difference that ‘double’ is making in this code.
It seems that when I replace it with ‘float’, there is absolutely no difference to the output. Is this correct?

My example:

int main(int argc, const char * argv[])
{
float postage;
postage = 0.60;

float packaging;
packaging = 1.33;

double PandP;
PandP = postage + packaging;

printf("With postage of %f and packaging of %f, the total shipping is GBP %f.\n", postage, packaging, PandP);

}

Many thanks

#11

In this specific case, yes; but in general, no.

There is a difference between a float and a double. To see the difference, insert these two lines in your main and run it:

printf ("A 'float' is %lu bytes long\n", sizeof (float));
printf ("A 'double' is %lu bytes long\n", sizeof (double));

A double can represent a bigger number with more precision than a float can.

#12

way late to the party , but some very good input in this thread. thanks all.