Fu Fundamentals of Pr Programming (Py Python) Floating-Point Operations Ali Taheri Sharif University of Technology Fall 2018
Outline 1. Fixed-Point Representation 2. Floating-Point Representation 3. Arithmetic Errors 4. The math Module 2 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Fixed-Point The number of bits before and after the decimal point is fixed ◦ Example: 98.75 3 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Floating-Point Supports a trade-off between range and precision IEEE 754 Standard: 𝒕𝒋𝒉𝒐𝒋𝒈𝒋𝒅𝒃𝒐𝒆 ∗ 𝒄𝒃𝒕𝒇 𝒇𝒚𝒒𝒑𝒐𝒇𝒐𝒖 4 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Arithmetic Errors When dealing with floating-point numbers, the result of calculation may not be necessarily correct: 0.1 + 0.1 + 0.1 == 0.3 7/5 - 1 == 2/5 1 - 1/3 == 2/3 You should be careful when doing comparisons: abs(a-b) < 1e-4 5 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
The math module Implements many of the IEEE functions for complex mathematical operations using floating point values, including logarithms and trigonometric operations. import math The import keyword enables us to include additional modules into our program 6 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Recommend
More recommend