division (original) (raw)


music

| OSdata.com: programming text book | | OSdata.com | | ---------------------------------------------------------------------------------------------- | | ---------- |

summary

This subchapter looks at division.

stub section

This subchapter is a stub section. It will be filled in with instructional material later. For now it serves the purpose of a place holder for the order of instruction.

Professors are invited to give feedback on both the proposed contents and the propsed order of this text book. Send commentary to Milo, PO Box 1361, Tustin, California, 92781, USA.

division

This subchapter looks at division.

Stanford C essentials

Stanford CS Education Library This [the following section until marked as end of Stanford University items] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.

Mathematical Operators

C includes the usual binary and unary arithmetic operators. See the appendix for the table of precedence. [Found in this book in the subchapter on order of precedence] Personally, I just use parenthesis liberally to avoid any bugs due to a misunderstanding of precedence. The operators are sensitive to the type of the operands. So division (/) with two integer arguments will do integer division. If either argument is a float, it does floating point division. So (6/4) evaluates to 1 while (6/4.0) evaluates to 1.5 -- the 6 is promoted to 6.0 before the division.

/ Division

Pitfall -- int vs. float Arithmetic

Here’s an example of the sort of code where int vs. float arithmetic can cause problems. Suppose the following code is supposed to scale a homework score in the range 0..20 to be in the range 0..100.

{
int score;
...// suppose score gets set in the range 0..20 somehow
score = (score / 20) * 100; // NO -- score/20 truncates to 0
...

Unfortunately, score will almost always be set to 0 for this code because the integer division in the expression (score/20) will be 0 for every value of score less than 20. The fix is to force the quotient to be computed as a floating point number…

score = ((double)score / 20) * 100; // OK -- floating point division from cast
score = (score / 20.0) * 100; // OK -- floating point division from 20.0
score = (int)(score / 20.0) * 100; // NO -- the (int) truncates the floating
// quotient back to 0

Stanford CS Education Library This [the above section] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.

end of Stanford C essentials

number systems

Here is a little bit of college level work on division.

A system of numbers os considered to be closed under an operation if it reproduces itself.

S is a set of numbers. S is closed under division if for any two numbers a and b in the set S, the quotient a / b (provided b ≠ 0) is also a number in the set S. A set that is closed under division and does not include zero (0) is called a multiplicative group.

The set of all positive real numbers is closed under division.

The set consisting of only the single number zero (0) is closed under division, even though division by zero is excluded (because it still fits the definition). Because of this special exception, the term multiplicative group specifically excludes zero.

A theorem regarding any set of numbers S, consisting of more than just the single number zero (0), that is closed under division, must have the following three properties:

(a) S contains the number one (1)

(b) When a ≠ 0 is a number in S the _a_-1 is also in S.

(c) S is always closed with respect to multiplication.

The set of all positive and negative powers {1, a, _a_2, …, _a_-1, _a_-2, …} for any number a ≠ 0 is a multiplicative group.

A system of numbers that is closed under addition, subtraction, multiplication, and division is called a field. Because all systems closed under subtraction are also closed under addition and all systems closed under division are closed under multiplciation, a field can be defined as a modul in which all elements after the exclusion of 0 form a multiplicative group..

Examples of fields are the set of only zero (a special case), the set of all rational numbers, the set of all real numbers, the set of all complex numbers, the set of all numbers of the form a + _b_√2 where a and b are integers, and the set of all numbers of the form a + b_√_D where D, a, and b are integers

assembly language instructions

For most processors, integer arithmetic is faster than floating point arithmetic. This can be reversed in special cases such digital signal processors.

On many processors, floating point arithmetic is in an optional unit or optional coprocessor rather than being included on the main processor. This allows the manufacturer to charge less for the business machines that don’t need floating point arithmetic.

The basic four integer arithmetic operations are addition, subtraction, multiplication, and division. Arithmetic operations can be signed or unsigned (unsigned is useful for effective address computations). Some older processors don’t include hardware multiplication and division. Some processors don’t include actual multiplication or division hardware, instead looking up the answer in a massive table of results embedded in the processor.

See also Integer Arithmetic Instructions in Assembly Language and Floating Point Arithmetic Instructions in Assembly Language


free music player coding example

Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

View music player in action: www.musicinpublic.com/.

Create your own copy from the original source code/ (presented for learning programming).

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.