Exercise 4

Added Exercise 4 to respository with README Files
This commit is contained in:
JirR02 2025-03-13 19:06:18 +01:00
parent 7b86ebfe6a
commit 03cc5aca86
8 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,19 @@
_This task is a text-based task but automatically checked. You are required to write your answers into solutions.txt by replacing the question marks with the correct solution. Please, do not change the structure of solutions.txt and be consistent with its syntax (comment-lines start with #)_
# Task
We examine the normalized binary representation of floating point numbers in the normalized floating point system $F*(2,4,-3,3)$.
1. Convert the following decimal numbers to the normalized binary representation. For each number, choose the appropriate exponent $e$ and round to the nearest value if you cannot represent the exact value. If the exact number falls exactly midway between two bracketing finite floating point representations, round to the number with an even least significant bit.
- $0.75_{10}$
- $3.1416_{10}$
- $2.718_{10}$
- $7_{10}$
- $0.11_{10}$
1. For each number of (2), convert the binary representation back to their decimal form, and determine the absolute rounding error of the conversion.
1. Calculate $2.718_{10} + 3.1416_{10} + 0.11_{10}$ in the binary representation. What do you observe?
**Note**: The question of how to round floating point numbers is non-trivial. The IEEE standard lists 5 different modes of rounding of which we only mentioned the most common one at the beginning of this exercise. If you are interested to learn more, you can access the most recent standard through the ETH network.

View File

@ -0,0 +1,60 @@
# NOTE: Lines starting with # are comments. Spaces are ignored.
# Replace the question marks, with your answer, following the instructions
# provided for each subtask.
######################## Subtask 1 ############################
# Subtask 1a: convert 0.75.
# In the given system, this is equal to "1.100 * 2^-1". The solution
# is written as "1.100*2^-1", without spaces and without quotation marks.
1a) 1.100*2^-1
# Subtask 1b: convert 3.1416
1b) 1.101*2^1
# Subtask 1c: convert 2.718
1c) 1.011*2^1
# Subtask 1d: convert 7
1d) 1.110*2^2
# Subtask 1e: convert 0.11
1e) 1.000*2^-3
######################## Subtask 2 ############################
# Convert the first number of previous Sub-Task back to decimal form.
# For each number you are required to write the decimal number and the
# conversion error (each on a different line).
# For your convenience, the first number is already converted.
2a-decimal) 0.75
2a-error) 0
2b-decimal) 3.25
2b-error) 0.1084
2c-decimal) 2.75
2c-error) 0.032
2d-decimal) 7
2d-error) 0
2e-decimal) 0.125
2e-error) 0.015
######################## Subtask 3 ############################
# Calculate the result of the addition and indicate the result in the
# same format as in the first sub-task ("siginificand*2^exp", without
# quotation marks). Then, write your observation as a comment right below.
3) 1.100*2^2
# Write your observations here
# The Decimal System cannot be represented accurately by the binary system. There will always be an error. When adding the number converted from the decimal to the binary sytem, the error becomes greater.

View File

@ -0,0 +1,17 @@
# Task
Consider a parabola $(\m{P})$ defined as $y = g(x)$, with $g(x) = 0.9 \cdot x^2 + 1.3 \cdot x - 0.7$.
Write a program that determines if a point $(x,y)$ lies on parabola $(\m{P})$ or not. The input is provided by two decimal numbers in the sequence $x,y$. The program must output `yes`, if the point lies on the parabola, otherwise `no`. Use datatype `double` for all variables and numbers used in the calculation of $g(x)$.
You will notice that a straight forward approach (comparing for equality) does not work, i.e., for some points that clearly should be on parabola $g$ such an approach returns result `no`.
_Hint_: Look at the difference between the exact values of the function and the values that your program calculates. Change the program so that it works properly for all points the submission system uses as test input without hard-coding the points. Expect an epsilon within the range $\[ 10^{-6}, 10^{-3}]\$. Experiment yourself to find the epsilon required to pass the test cases.
**Note**: Output only with `std::cout << "no" << std::endl;` or `std::cout << "yes" << std::endl;`, as the autograder will only accept output that exactly matches `yes\n` or `no\n`. For all other messages, use `std::cerr` as in:
```cpp
std::cerr << "This is a test message\n"
```
Those will be ignored by the autograder.

View File

@ -0,0 +1,25 @@
#include <iostream>
int main() {
double x = 0; // x inputed by user
double yin = 0; // y inputed by user
double ycon = 0; // y control
double emax = 0.0001;
double emin = -0.0001;
std::cin >> x;
std::cin >> yin;
ycon = 0.9 * x * x + 1.3 * x - 0.7;
std::cerr << ycon;
std::cerr << yin - ycon;
if (yin == ycon || (yin - ycon <= emax && yin - ycon >= emin))
std::cout << "yes";
else
std::cout << "no";
return 0;
}

View File

@ -0,0 +1,16 @@
# Task
Implement the following rounding function that rounds a 64-bit floating point number (type `double`) to the nearest 32-bit integer (type `int`). You may assume that the type `double` complies with the IEEE standard 754. The function is only required to work correctly if the nearest integer is in the value range of the type `int`, otherwise, the return value of the function is undefined.
**Note: Usage of library rounding functions (standard or others) is not allowed.**
```cpp
// PRE: x is roundable to a number in the value range of type @int@
// POST: return value is the integer nearest to x, or the one further
// away from 0 if x lies right in between two integers.
int round_number(double x);
```
Write your solution in `rounding.h`.
_Hint_: In `C++`, when you convert a `float` or `double` to an `int`, the fractional part gets cut off (truncated). Think about how you can use the truncated number to solve the exercise.

View File

@ -0,0 +1,13 @@
#pragma once
// PRE: x is roundable to a number in the value range of type int
// POST: return value is the integer nearest to x, or the one further
// away from 0 if x lies right in between two integers.
int round_number(double x) {
int res;
if (x < 0)
res = x - 0.5;
else
res = x + 0.5;
return res;
}

View File

@ -0,0 +1,7 @@
# Task
Write a program that performs the binary expansion for a given decimal input number $x$, where $0 \leq x < 2$. Use the algorithm presented in the lecture. The program must output the first $16$ digits of the number in the format: $b_0, b_1, b_2, ... , b_15$.
_Important_ Always print all $16$ digits, even the trailing zeros. Do not normalize or round the number.
You can structure your program into functions to avoid code repetition. Do not forget to annotate functions with pre- and post conditions.

View File

@ -0,0 +1,28 @@
#include <iostream>
int main() {
float i;
int n;
float d;
std::cin >> i;
if (i < 0 || i > 2)
return 0;
else {
n = i;
d = i - n;
std::cout << n << ".";
n = d;
d = 2 * (d - n);
for (int j = 1; j <= 15; ++j) {
n = d;
d = 2 * (d - n);
std::cout << n;
}
}
}