It is really hard to understand some new concept or function for new programmers like me. On meter to feet and inch conversion the author did not discuss about floor function. The part i was not able to understand was "unsigned int feet = (unsigned int)floor(rawFeet); ". After thinking long time i figured it out what is actually going on in this code which i want to share with new programmers like me. The “floor function” converting rawFeet into an integer and storing that integer in *ftptr. example if meter is 1 then feet is 3 excluding .281.
Thank you! I was having a hard time understanding.
I was also confused by this. When I looked it up, I found that “the floor function returns the greatest integer not greater than x. For example if the input is 2.25 then output will be 2.00”. Documentation: programmingsimplified.com/c/math.h/floor
I agree that this part of the book is very confusing. Some points are explained in excruciating detail while others (like the floor function) are completely left to the reader to figure out.
Also, for the “(unsigned int)” part of the code, what did that mean? Were we just establishing the type for the floor? And but didn’t we already do that before the equal sign?
I have to agree that this chapter (well and some of the previous chapter) was a complete left turn compared to the rest of the beginning of the book.
I was able to understand the whole book up until this point or if I did not get the concepts write away I was able to step back and walk myself through how to arrive where I needed to be. This chapter just seemed to be like all of the other books that I have tried to read while attempting to learn how to program. Here is a program, you won’t understand it but you need to.
I made a note that I didn’t really understand the concepts and I need to go back and read it again and walk through it, but for now I am going to move on and see if I can get by.
I have the same exact problem here. Had no idea what this line of code was all about.
unsigned int feet = (unsigned int) floor(rawFeet);
Had to search around and found out what "floor() is used for.
To simplify things, I converted all “unsigned int” to “int” and removed the (unsigned int) before the “floor(rawFeet);” and the result was the same.
double rawFeet = meters * 3.281;
int feet = floor(rawFeet);
I don’t understand why we have to add “(unsigned int)” before the “floor(rawFeet);” as it does not seem to be necessary. Can someone explain this to me?
That’s explained on page 45 in the 1st Edition of the book. (Look up the cast operator in the index.)
The expression “b bar[/b]” means convert the value of bar (whatever its type is) to a value of type Foo. (In the expression, pair of parentheses b[/b] hugging Foo is the type conversion operator.)
These days, compilers are smart and are able to convert a value of one type to a value of different type whenever it makes sense to do so; because of this, most of the time you don’t have do the conversion explicitly. However, there are times you may need to do it explicitly.
For example:
const int MinusOne = -1;
if (MinusOne < 0) {
// Of course, yes
}
if (MinusOne > 0) {
}
else {
// Of course, not!
}
if ((unsigned long)(MinusOne) > 0) {
// Yes!
}
[Become a competent programmer faster than you can imagine: pretty-function.org]
Thanks to everyone for contributing additional research and explanation. It helped me to understand the use of “floor” in that code example. The lack of explanation about that code example is such a glaring omission that it seems like it must be an unintentional mistake. The rest of the book prior to that point did such a great job at explaining all of the code it presents. I’m still working my way through the book so hopefully the rest of it will continue being clear and thorough with its explanations.