(4) Arithmetic Expressions Compatibility Mode

(4) Arithmetic Expressions Compatibility Mode - The Speed...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman 2008 The Speed of Light ± How long (in minutes) does it take light to travel from the sun to earth? 2008 Expressions 2 The Speed of Light ± How long (in minutes) does it take light to travel from the sun to earth? ± Light travels 9 46 x 10 12 km a yea 2008 Expressions 3 Light travels 9.46 x 10 km a year ± A year is 365 days, 5 hours, 48 minutes and 45.9747 seconds long ± The average distance between the earth and sun is 150,000,000 km Elapsed Time program light_travel implicit none real :: light_minute, distance, time real :: light_year = 9.46 * 10.0 ** 12 2008 Expressions 4 light_minute = light_year / (365.25 * 24.0 * 60.0) distance = 150.0 * 10.0 ** 6 time = distance / light_minute "minutes to reach earth." end program light_travel Arithmetic Expressions An arithmetic expression is formed using the operations: + (addition) 2008 Expressions 5 - (subtraction) * (multiplication) / (division) ** (exponentiation) Watch out for ambiguity Let’s look at an expression from our program distance = 150.0 * 10.0 ** 6 2008 Expressions 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Watch out for ambiguity Let’s look at an expression from our program distance = 150.0 * 10.0 ** 6 2008 Expressions 7 What value is assigned to distance? (150.0 * 10.0) ** 6 ? Watch out for ambiguity Let’s look at an expression from our program distance = 150.0 * 10.0 ** 6 2008 Expressions 8 What value is assigned to distance? (150.0 * 10.0) ** 6 ? 150.0 * (10.0 ** 6) ? Watch out for ambiguity How about this expression? light_minute = light_year / (365.25 * 24.0 * 60.0) 2008 Expressions 9 Watch out for ambiguity How about this expression? light_minute = light_year / (365.25 * 24.0 * 60.0) 2008 Expressions 10 What if the expression didn’t have parentheses? light_minute = light_year / 365.25 * 24.0 * 60.0 Precedence Rules ± Every language has rules to determine what order to perform operations ± These rules try to mimic the 2008 Expressions 11 conventions we learn growing up ± For example, in FORTRAN ** comes before * ± In an expression, all of the **’s are evaluated before the *’s Precedence Rules ² First evaluate operators of higher precedence 3 * 4 – 5 Æ ? 3 + 4 * 5 Æ ? 2008 Expressions 12
Background image of page 2
3 Precedence Rules ± First evaluate operators of higher precedence 3 * 4 – 5 Æ 7 3 + 4 * 5 Æ 23 2008 Expressions 13 Precedence Rules ± First evaluate operators of higher precedence 3 * 4 – 5 Æ 7 3 + 4 * 5 Æ 23 2008 Expressions 14 ± For operators of the same precedence, use associativity. Exponentiation is right associative, all others are left associative 5–4–2 Æ ? 2 ** 3 ** 2 Æ ? Precedence Rules ± First evaluate operators of higher precedence 3 * 4 – 5 Æ 7 3 + 4 * 5 Æ 23 2008 Expressions 15 ± For operators of the same precedence, use associativity. Exponentiation is right associative, all others are left associative 5–4–2 Æ -1 (not 3) 2 ** 3 ** 2 Æ 512 (not 64) Precedence Rules
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

(4) Arithmetic Expressions Compatibility Mode - The Speed...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online