ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

Thanks again guys!

I've been reading the "Chapter 4 Variables, Data Types, and Arithmetic Expressions" and a lot of it does make enough sense now, but there are a few things I'm not too sure about. I done the exercises I could, but the last ones I'm not really too sure about.

When it says something like "1.7e4", which according to the C pdf is scientific notation; does the "e" mean "exponent" ?

BTW RT, the typo in your post #136, about floating point numbers, I fixed. But I kept forgetting to mention before that I saw that before you corrected it, and fixed it.

Code:

#include <stdio.h>
int main(void)
{
int a = 1;
float b = 1;
int test_int_result;
float test_float_result;
test_int_result = a / 2;
test_float_result = b / 2;
printf("Test result of dividing an integer by 2: %d / 2 = %d\n", a, test_int_result);
printf("Test result of dividing a float by 2: %.01f / 2 = %.01f\n", b, test_float_result);
return 0;
}
/* The error I got when I forgot to add the "#include <stdio.h>" header below:
*
* [james@jamespc math-helpers]$ gcc -Wall -Werror floating_point_numbers.c -o floating_point_numbers
floating_point_numbers.c: In function ‘main’:
floating_point_numbers.c:12:4: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration]
printf("Test result of dividing an integer by 2: %d / 2 = %d\n", a, test_int_result);
^
floating_point_numbers.c:12:4: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror]
cc1: all warnings being treated as errors
*/

Chapter 4 Exercises

(I haven't included some of the details below, because the post is long enough as it is.)

Quote:

2. Which of the following are invalid variable names? Why?

6_05, _1312 - because you cannot use numbers (digits) in variable names.

_ - because there should be something after this.

Quote:

3. Which of the following are invalid constants? Why?

0X0G1, 0XABCDEFL - because hexadecimals only go from A to F.

0x10.5 - because the dot is not a valid hexadecimal character.

15,000 - because you cannot use commas in constants, it would have to written as 15000 without the comma.

Quote:

4. Write a program that converts 27° from degrees Fahrenheit (F) to degrees Celsius
(C) using the following formula:

C = (F - 32) / 1.8

Code:

#include <stdio.h>
int main(void)
{
int F = 27;
float C = (F - 32) / 1.8;
printf("Fahrenheit (F) to degrees Celsius: %f\n", C);
return 0;
}

Quote:

5. What output would you expect from the following program?

It would display the character (char) "d" on the screen.

Quote:

6. Write a program to evaluate the polynomial shown here:

3 2
3x - 5x + 6

for x = 2.55.

I'm not sure what the "3x" with the little "3" above it, and the "5x" with the little "2" above it mean? Also, I have no idea what "for x = 2.55." means.

Quote:

7.Write a program that evaluates the following expression and displays the results
(remember to use exponential format to display the result):
(3.31 x 10-8 x 2.01 x 10-7) / (7.16 x 10-6 + 2.01 x 10-8)

Not sure what the little "-8" and "-7" mean? The "x" means multiply, doesn't it? I tried to read that link in post #151, but it doesn't make much sense to me, sorry.

Quote:

8.To round off an integer i to the next largest even multiple of another integer j,
the following formula can be used:

Next_multiple = i + j - i % j

For example, to round off 256 days to the next largest number of days evenly
divisible by a week, values of i = 256 and j = 7 can be substituted into the pre-
ceding formula as follows:

Next_multiple

= 256 + 7 - 256 % 7
= 256 + 7 - 4
= 259

Write a program to find the next largest even multiple for the following values of i and j:

i j
365 7
12,258 23
996 4

I'm really not sure about this one(s) ?

Like I said before, I tried to do as much as I could, but I really am not sure about the last couple of questions apart from what's obvious.

1.) Most of your problems are understanding exponents. Research that topic more. The link offered by Andy Alt is excellent and 100% on the mark. If you have trouble with the topic, then web search for more resources that can explain it in ways which are easier for you to understand.

2.) I wouldn't get too hung up on recognizing good vs. bad variable names. The compiler will flag an error when you've used an illegal name.

Caviat:

- Interpreting a lengthy list of compiler warnings and errors can be an exercise in detective work.
- There are some really bad things a programmer can do, such as to use a C keyword as a variable.
- For instance, what if you named a variable "if", or "switch", or "float"?
- These are all special terms which mean something as part of control flow.
- You can't use them as variables, and the compiler will complain about them.
- But the error may not be exactly "You can't use 'switch' as a variable!"
- The error may say something like, "missing '(' identifier before 'switch'", or something like that.
- Eventually you'll realize that the use of 'switch' as a variable name is not allowed.

3.) % is the C modulus operator. Look it up, look up examples. And really. Because if you have difficulties with the 5-10 word description, that's about what I'd reiterate here, so that's not going to help. Read the description, and look at examples, then re-read the description, or other descriptions, and look at more examples. It's not rocket science, it is a rule applied to calculations. Once you learn it, you learn it. Until you do, you won't understand what code using that operator is doing.

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

So does the little minus sign with the little number just mean it's a negative number instead of a positive number, but it's still multiplying that little number? Also, does the "x" still just mean multiply? As I'm still not 100% clear on it. I DID try reading other links.

Also, does the "C modulus operator" just mean it gives the remainder and that's it? I DID look at some examples, but that's the only thing I'm getting from it.

Is that all there is to it?

The following link is what I looked at as well as the link posted previously;

So does the little minus sign with the little number just mean it's a negative number instead of a positive number, but it's still multiplying that little number? Also, does the "x" still just mean multiply? As I'm still not 100% clear on it. I DID try reading other links.

If you by "little number" you mean the exponent with a leading minus sign, then no. Go back to the link you posted and look down the page to Negative Exponents, what does it tell you? That page looks very clear to me (in fact the site looks very well done).

When communicating to others about exponents in a format like this forum that does not support superscript fonts (the smaller raised font usually used for exponents), learn to use the the common alternatives:

Code:

8^2
8e2
8E2

All of which mean an 8 with an exponent of 2, meaning 8 squared or 8 to the second power. There is no meaning in 'e' vs 'E', personal preference.

For negative exponents simply prefix the exponent with a minus sign...

Code:

8^-2
8e-2
8E-2

Bonus point question: Why is 8^2 called "eight squared"?

Quote:

Originally Posted by jsbjsb001

Also, does the "C modulus operator" just mean it gives the remainder and that's it? I DID look at some examples, but that's the only thing I'm getting from it.

Is that all there is to it?

Well, yes and no...

Yes, the modulo operator shows the remainder of one number divided by another - that is exactly what it does...

Code:

8 % 5 = 3

Is that "all there is to it"? Yes, but that is a lot when you come to an understanding of how that can be useful!

So, I would say no, that is not "all there is to it" as in, "That is so simple I can forget about it".

Each little bit stands in relation to the whole in very important ways. Math is a structure of meaning, represented by a notation. You must learn both the notation and the meaning.

It is really just going to take time and effort. As rtmistler suggested, all that others can do is answer your questions with variations of the same simple words you find in books and in online places like the mathisfun site. Actually understanding what it means can only be done by you, there is no short-cut path...

Quote:

When Ptolemy I (ruler of Egypt from 323 BCE – 283 BCE) grew frustrated at the degree of effort required to master geometry via Euclid’s Elements, he asked Euclid whether there was some shorter path. The great mathematician is said to have replied “There is no royal road to geometry.” (found here)

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

Yeah, sorry, I didn't read about "Negative Exponents" properly. So it just means that instead of multiplying you're dividing instead, correct?

Thanks astrogeek for explaining about the "common alternatives" for writing "exponents", as it really was starting to get quite confusing.

Bonus point question answer: According to this, it means to multiply the same number by itself, correct? Beyond that, I've got no idea bloody idea.

I'll look up "notation" and "meaning" in relation to mathematics later on, as I'm all "mathed out" today. I'm sorry, but this maths is absolutely killing me. As I now understand why I had absolutely zero interest in it while I was at school. I think I'll just understand what things mean rather than knowing all the answers if that makes sense, as this is just getting more and more depressing. I think I'll also just stick to the "mathsisfun" site, as it's about the only site that seems to explain things in an understandable way to me.

I'll look up "notation" and "meaning" in relation to mathematics later on, as I'm all "mathed out" today. I'm sorry, but this maths is absolutely killing me. As I now understand why I had absolutely zero interest in it while I was at school. I think I'll just understand what things mean rather than knowing all the answers if that makes sense, as this is just getting more and more depressing. I think I'll also just stick to the "mathsisfun" site, as it's about the only site that seems to explain things in an understandable way to me.

PS: Maths is NOT fun, pigs you know what it is!

Programming is nothing but math. The programming language of choice is just a specific notation for math for a class of problems.

It is true one does not need to be a PhD in Mathematics in order to be able to program. I can attest that there are some points in Math which go too far beyond my tolerance level. But meanwhile I've also learned that the rules or laws of Math are already very well concluded. Therefore if I'm dealing with a calculation that is complex, and I have someone more invested in those details telling me the algorithm, then I can code it.

When writing C; one can't really escape needing to understand the intricacies of pointers, the arithmetic for those pointers, and the various notations for them. And things like operators. Operators can be logical or bitwise, and also there are operators which do certain functions, and they are used a lot. Such as the modulus operator, or the complement operator, ~.

So these are everyday things. If someone doesn't understand them, or generally what they do, then they're going to be behind the curve. One very common thing is to have a bit mask and use that to either set a value, clear a value, or test a value, and many times one uses |=, or ^=, or &= along with ~ . Understanding that you have 32-bits and that is eight F's, 0xffffffff is important. You have to understand what really happens when you divide that by 2, or 4, or 8, or what happens when you invert it or if you want to clear a certain bit in that term, and how you do it.

I don't know whether you really, truly need to know exponential numbers very well for this. Why? Because it was hammered into my head during Elementary School and Junior High. At some point, we all knew it well enough that they didn't bother re-teaching it to us. But we also could do Chemistry which uses a lot of that notation, so we continued to use it. You've said you're missing that.

Exponents are sort of a building block for understanding the different bases of numbers and understanding the value of variable and expressions when they are written in Hex, Binary, or Octal. I don't spend my day talking in exponential notation, but I can assure you that I know what 2^16 represents, or 2^32, because I need those number many times. Fact is I need 2^8 a far greater amount of time because I deal a ton with byte sized operations. Yes, 2^8 represents a byte of 8-bits and it's maximum value for unsigned is 255, it's maximum value for signed is -127 to +127.

I have to know that if I divide something by 4, it is the same as a bit shift of that variable two positions to the right. These manipulations I'm talking about are slight uses of altering a number, by way of changing the exponential value of it. Very slight, however one does need to know the basics in order to be able to manage the design of their code.

So I can't tell you where to draw that line, with the exception that things like the examples they're covering with you that use oddball numbers like 2.44E-7 are NOT what I generally would deal with unless I was performing scientific calculations all the time.

Instead what I need to understand is:

I have a bit mask, and bit #15 needs to be cleared.

Let's say the value is 0xFFF0FABC
Bit #15 is defined as 0x00008000
To clear that bit I'd do something like: result = 0xFFF0FABC & ~0x00008000
Giving me an actual result of: 0xFFF0EABC

If you can't understand that, then figure out how to understand that, because it's pretty huge in C programming.

And by the way, the "normal" way that would look is:

Code:

#define OPERATOR_BIT_15 0x00008000
uint32_t value, result;
value = input_argument; // also a uint32_t type variable of some unknown value
// Clear OPERATOR_BIT_15
result = value & ~(OPERATOR_BIT_15);
return result;

That is a very typical world that I live in on an everyday basis with C. Yes, it has to do with understanding what 2 "to the <something>" means, or understanding it in Hex. It doesn't have to do exactly with understanding the "quantitative" answer. Such as I really have ZERO idea what 2^15 exactly is. Sure I can think about it for some period of time and then conclude the answer, and then promptly check it on a calculator. Bottom line is I don't care, or don't need to care, instead I need to know that it is "bit 15", AND I need to recognize that it is set versus not, when I break my code in the debugger and examine a variable. So I can go, .... "Well, THAT's not right!" Turn to my colleague and go, "WHAT'S WRONG WITH YOUR #$@%!! HARDWARE NOW?!?!?" <engineering argument ensues> It's all in fun, we live for it.

When communicating to others about exponents in a format like this forum that does not support superscript fonts (the smaller raised font usually used for exponents),

The superscript font can be faked with Unicode: 8²

Quote:

Code:

8^2
8e2
8E2

All of which mean an 8 with an exponent of 2, meaning 8 squared or 8 to the second power. There is no meaning in 'e' vs 'E', personal preference.

You are of course correct, the 'e' notation is usually interpreted as a power of ten multiplier, not as a power of the number. Also commonly called scientific notation.

Which again demonstrates the necessity to understand both the notation and the meaning, to be able to ask the questions and to understand the answers!

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

Thanks again guys!

Thank you for your post as well ntubski, as it forced me to re-look at the "mathsisfun" site about "exponents". While I will admit that I couldn't understand what you were saying about the way you interpret the symbols used for "exponents"; I did work out what you meant by what you were saying, in that for "8e2" (and 8E2), you meant 10 * 10 = 100, and then 8 * 100 which equals 800, correct?

Assuming I'm right in what I said above, I think I'll move on with the C book/pdf, and I was having a look at "for" loops, and while most of it seems to make sense; I had a bit of a play around with it, and I'm not sure I understand it 100%.

I got two different results depending on what I done with the code;

Now clearly in the second example it still does the loop, but gives me the output for each time it's added "5" to the initial value with the line of code I commented out. But in the first example, it just gives me the end result, but still does the loop.

I'm not even really sure how to ask the question I'm trying to, but I'm just at a bit of a loss to fully understand what's exactly happening here apart of the obvious. I did try re-reading the C book/pdf, as well as playing around with the above examples, but I'm still not fully understanding why I need the line below the for statement line (numberToAdd = numberToAdd + init_value;) to give me just the one line output - if that makes any sense at all. I understand WHAT that lines does (I *think*), but I'm just not getting a clear picture of exactly what's going on here. Sorry if my question doesn't make any sense - I just don't know how else to ask it.

I did work out what you meant by what you were saying, in that for "8e2" (and 8E2), you meant 10 * 10 = 100, and then 8 * 100 which equals 800, correct?

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

Thanks jpollard, I think I now understand what I wasn't before. Being; that extra line (numberToAdd = numberToAdd + init_value;) adds another "5" to the initial value (being "1"), I'm sure this is what I wasn't understanding. As I terminated the line for the loop itself, then run it, then un-terminated it, and uncommented the extra line, then ran it again, and I could see the difference. So thanks again, your pointer was just what I needed, thanks.

Thank you ntubski for confirming that, it's also just what I needed.

I've been having another look at octal and hexadecimals and I think I now know where I was going wrong; I wasn't understanding that I need to go back to zero, for octal once I reach "7", and for hexadecimals once I reach "16". So it's the same problem I was talking about before; I just could not put my brain into reverse, because of my subconscious, because it's just not used to it. So I think I'm really beginning to understand octal and hexadecimals a lot better now. I *think* I now finally completely understand astrogeek's post #142 now, it just wasn't making sense before with the "+1", because I wasn't understanding the "go back to zero" part, which caused the confusion for me.

The only thing that might be added is the number "0". Base 10 goes 0 1 2 3 4 5 6 7 8 9; hence 10 values for a digit.
Octal (base 8) goes 0 1 2 3 4 5 6 7; and hex (base 16) go 0 1 2 3 4 5 6 7 8 9 A B C D E F.

Incidentally, this continues with binary: 0 1.

Conversion to a given base starts by dividing the number by the desired base. The remainder is the current digit on the right... the quotient is what is still to be converted. Dividing that again, and the remainder is again the digit (to be placed to the left of the previous digit.

if using base 16 (or any base larger than base 10) you have to look up the glyph that corresponds. Using base 16 the decimal number 28, becomes 38/16 => 1 remainder 11, and the eleventh symbol in base 16 is C. If you divide the quotient again becomes 1/16 => 0 with a remainder of 1; and the corresponding symbol in base 16 is 1.

-------

Where things can get fun is that base 2, base 8, and base 16 are very commonly used, the concept can also be done for really strange bases - like 50.

What gets fun is that this base has enough symbols for all of the latin alphabet (26 symbols) with a good bit left over...

If you then assign the symbols " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.0123456789" (yes the first symbol is a space), then suddenly, you have a compression - you get to store three symbols in a 16 bit number.

In the same way of converting bases, if the remainder is 0, then the corresponding symbol is a space; if the remainder is a 1 then then symbol is "A" ... and you get to represent text in a compressed form...

DEC used this to save space in filesystems for 12 bit, 16 bit, 18 bit, 32 bit and 36 bit computers (some using a slightly different set of symbols) to save space for file names (from around 1959 to the end of Digital Equipment Corporation).

The result of using radix 50 allows 8 characters per name, a period, and 3 more characters for an extension all uppercase. On the byte systems (the PDP 11, a 16 bit machine and up) this allowed a file name to be compared with a minimum of instructions - as a single 4 byte (32 bit) value could hold an entire file name. And that improved performance by reducing the time it takes to find files.

And the characteristics of that filesystem (the 8 character name, a period, and a three character extension) showed up almost everywhere else at the time. It is where Microsoft got the 8.3 form for FAT/VFAT filesystems.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.