Reference.Arithmetic History

Hide minor edits - Show changes to markup

February 13, 2008, at 05:43 PM by David A. Mellis -
Changed lines 5-6 from:

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type. If the operands are of different types, the "larger" type is used for the calculation.

to:

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an int with the value 32,767 gives -32,768). If the operands are of different types, the "larger" type is used for the calculation.

Added lines 35-36:
  • Know that integer constants default to int, so some constant calculations may overflow (e.g. 60 * 1000 will yield a negative result).
May 29, 2007, at 03:24 AM by Paul Badger -
May 29, 2007, at 03:23 AM by Paul Badger -
Changed line 7 from:

If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the operation.

to:

If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.

May 29, 2007, at 03:21 AM by Paul Badger -
Changed lines 5-6 from:

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type. If the operands are of different types, the "larger" type is used for the calculation.

to:

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type. If the operands are of different types, the "larger" type is used for the calculation.

If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the operation.

Changed lines 33-34 from:

Tips:

to:

Programming Tips:

Changed lines 41-47 from:
  • Use the cast operator e.g. (int)myfloat to convert one variable type to another on the fly.

to:
  • Use the cast operator e.g. (int)myFloat to convert one variable type to another on the fly.

April 16, 2007, at 05:49 PM by Paul Badger -
Changed lines 42-45 from:
to:

April 16, 2007, at 05:18 AM by Paul Badger -
Changed lines 38-41 from:
  • Use the cast operator eg (int)myfloat to convert one variable type to another on the fly.

to:
  • Use the cast operator e.g. (int)myfloat to convert one variable type to another on the fly.

April 16, 2007, at 05:11 AM by David A. Mellis -
Changed lines 5-6 from:

The arithmetic operators work exactly as one expects with the result returned being the result of the two values and the operator

to:

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type. If the operands are of different types, the "larger" type is used for the calculation.

Changed lines 17-18 from:

result = value1 [+-*/] value2

to:
result = value1 + value2;
result = value1 - value2;
result = value1 * value2;
result = value1 / value2;
Changed lines 26-28 from:

value1: any variable type

value2: any variable type

to:

value1: any variable or constant

value2: any variable or constant

April 15, 2007, at 11:13 PM by Paul Badger -
Added lines 35-39:
April 15, 2007, at 11:11 PM by Paul Badger -
Changed lines 26-29 from:

A longer tutorial on computer math can eventually go in this space but for now, to benefit beginning programmers some general guidelines will be presented. These will hopefully get you started toward getting the same answer out of your Arduino that you do on your calculator.

  • Choose variable sizes that you are sure are large enough to hold the largest results from your calculations
to:
  • Choose variable sizes that are large enough to hold the largest results from your calculations
Changed lines 32-34 from:
to:
  • Use the cast operator eg (int)myfloat to convert one variable type to another on the fly.

April 15, 2007, at 11:08 PM by Paul Badger -
Changed lines 28-30 from:

to:
  • Choose variable sizes that you are sure are large enough to hold the largest results from your calculations

  • Know at what point your variable will "roll over" and also what happens in the other direction e.g. (0 - 1) OR (0 - - 32768)

  • For math that requires fractions, use float variables, but be aware of their drawbacks: large size, slow computation speeds

April 15, 2007, at 11:03 PM by Paul Badger -
Changed line 21 from:

value1: any variable type

to:

value1: any variable type\\\

Changed lines 26-32 from:

For beginning programmers there are several details of doing math on the computer to which one must pay attention. One is that math on computers, as opposed to algebra class, must exist in physical space. This means that the variable (which occupies a physical space on your Atmega chip) must be large enough to hold the results of your calculations.

Hence if you try something like this

 byte x;
x =  255;
x = x + 1; 
to:

A longer tutorial on computer math can eventually go in this space but for now, to benefit beginning programmers some general guidelines will be presented. These will hopefully get you started toward getting the same answer out of your Arduino that you do on your calculator.

April 15, 2007, at 10:59 PM by Paul Badger -
Added lines 14-18:

Syntax

result = value1 [+-*/] value2

April 15, 2007, at 10:27 PM by Paul Badger -
Changed line 8 from:
to:

[@

Changed line 13 from:
to:

@]

Changed lines 26-27 from:

x = x + 1;

to:

x = x + 1; @]

April 15, 2007, at 10:23 PM by Paul Badger -
Added lines 1-27:

Addition, Subtraction, Multiplication, & Division

Description

The arithmetic operators work exactly as one expects with the result returned being the result of the two values and the operator

Examples

y = y + 3; x = x - 7; i = j * 6; r = r / 5;

Parameters:

value1: any variable type value2: any variable type

Tips:

For beginning programmers there are several details of doing math on the computer to which one must pay attention. One is that math on computers, as opposed to algebra class, must exist in physical space. This means that the variable (which occupies a physical space on your Atmega chip) must be large enough to hold the results of your calculations.

Hence if you try something like this [@ byte x; x = 255; x = x + 1;

Share