程序代写代做 C # Questions / Tasks

# Questions / Tasks

## Overview

These floats won’t do! We found some ancient arcane computers with
lots of neat data but it is in weird floating point formats that C
doesn’t support! What will we do? I know, we crowd source the solution
and test the students on their ability to address arbitrary floating
point formats!

You are to develop a system for calculating arbitrary floating point
numbers. We have distributed to you a tarball containing the
assignment definition. It contains starter header and .c files that
describe what kind of floating point number you are dealing with. You
must operate on floating point numbers with these constraints.

Your floating point numbers will have at least 2 bits of exponent and
at least 1 bit of mantissa/fraction/significand. They will have a size
of 16 or 32 bits. This will be called MyFloat. You will implement MyFloat.

You will implement:

* Q1: Binary floating point representation of your MyFloat.
* Q2: Binary floating point operations for MyFloat such as addition,
subtraction, multiplication, and division.
* Q3: String input and output of floating point numbers represented by
MyFloat and to be represented by MyFloat.
* Q4: Binary floating point operations for MyFloat such as addition,
subtraction, multiplication, and division without the help of any
floating point routines.
* Q5: A working Makefile that builds and tests your solutions.

Floating point numbers will be defined by a floating point definition
that will describe:
* Size (16 or 32 bits)
* Sign bit (always 1 sign bit)
* Exponent (2 or more bits)
* Mantissa (1 or more bits)

The mantissa will always include 1 implicit bit that is not saved or
written. Thus a mantissa of 0b001 is actually 0b1001. Remember that a
an IEEE754 single precision float is 32 bits, 1 sign bit, 8
exponential bits, 23 mantissa bits (+1 implied).

Your floating point numbers will support:

* +0 and -0 — where all bits are zero or just the sign bit
* -inf and +inf — where all the exponent bits are 1 and the mantissa bits are 0

## Question 1: Binary floating point representation of your MyFloat.

Your goal is to implement the binary representation of your `MyFloat`
data-structure.

You have to represent `0`,`-0`, `1.0`, `-infinity`, `infinity`, and
handful of real numbers with your new float: `MyFloat`.

Zero is supposed to have all bits set to `0`.

Infinity is where the exponent has all bits set to `1` and the mantissa
is all set to `0`.

`1.0` will be represented by the exponent set to BIAS/2 (integer
division) (`pow(2.0,0)`).

The file `question1_test.c` contains tests of your binary floating
representation that you implement within `myfloat.c` and `myfloat.h`.

The files `myfloat.h` and `myfloat.c` describe what needs to be
implemented for question 1 in great detail. Your goal is to implement
that functionality to represent a MyFloat to pass `question1_test.c`

Please review the headers of `myfloat.c` and `myfloat.h` as a more
in depth description of what is required is within those files.

1. Read `myfloat.h`.
1. Read `myfloat.c`.
1. Write your name, ccid, and student number into `myfloat.h`.
1. Write your name, ccid, and student number into `myfloat.c`.
1. Modify the `makefile` to compile `question1_test` correctly.
1. Create the datastructure in either `myfloat.h` or `myfloat.c`.
1. Implement `myfloat_zero()`
1. Implement `myfloat_one()`
1. Implement `myfloat_negative()`
1. Implement `myfloat_exponent()`
1. Implement `myfloat_mantissa()`
1. Implement `myfloat_set_negative()`
1. Implement `myfloat_set_exponent()`
1. Implement `myfloat_set_mantissa()`

Watch out for unwanted padding. To tightly pack structs add this line to the struct definition:
“`
struct __attribute__((packed)) coolbear { char a; char b; }; // A tightly packed coolbear
“`

You should pass:
“`
make clean run-question1_test
make CC=clang clean run-question1_test
make CC=clang ARCH=-m32 clean run-question1_test
make lint-myfloat.c
make valgrind-question1_test
“`

### Hints and Pro-tips

* Bitfields, shifting, and bitwise masks work fine. Be sure to get the
order right.
* If you use structs and they add padding add this to your struct
definition to tightly pack your struct `__attribute__((packed))`.
This is important to make short-sized structs.

### Marking for Q1

– [ ] **2 Points** All question1_test passes — no failed assertions
– If and only if all tests pass the following marks are available:
– [ ] **1 Point** All tests pass with -m32 (32bit mode) and -m64 (64bit mode)
– [ ] **1 Point** All tests pass with clang
– [ ] **2 Points** `myfloat.c` and `myfloat.h` lint properly and meet all other quality standards, listed above.

Remember to upload to eclass after making any progress on Q1.
You can upload as many times as you want before the deadline.

## Question 2: Binary floating point operations for MyFloat such as addition, subtraction, multiplication, and division.

You are to implement binary floating point operations for MyFloat such
as addition, subtraction, multiplication, division, and reciprocal.

To add floating point numbers you should align their mantissas and add
the numbers. If there is an overflow of the mantissa you should
increase the exponent of the result. Lazy implementations that
normalize the floating point numbers to the same exponent and add the
shifted mantissa are perfectly valid. Remember to include the implied
bit when you do it. Numbers that are too big or too small will not
change after addition. Do not worry about rounding.

To multiply floating point numbers you should add their exponent and
then integer multiply their mantissas. Remember to include the implied
bit when you do it.

To divide floating point numbers you should subtract their exponents
and then integer divide their mantissas. Remember to include the implied
bit when you do it.

To invert a floating point number you should subtract the exponent
from the exponent of 1.0 and you should integer divide 1.0’s mantissa
(with implied) by your number you are inverting. Remember to include
the implied bit when you do it.

If you really want to, you can use doubles to do your dirty work for
you.

1. Read `myfloat_ops.h`.
1. Read `myfloat_ops.c`.
1. Write your name, ccid, and student number into `myfloat_ops.h`.
1. Write your name, ccid, and student number into `myfloat_ops.c`.
1. Modify the `makefile` to compile `question2_test` correctly.
1. Implement `myfloat_add()`
1. Implement `myfloat_sub()`
1. Implement `myfloat_mul()`
1. Implement `myfloat_inv()`
1. Implement `myfloat_div()`

You should pass:
“`
make clean run-question2_test
make CC=clang clean run-question2_test
make CC=clang ARCH=-m32 clean run-question2_test
make lint-myfloat.c lint-myfloat_ops.c
make valgrind-question2_test
“`

### Pro-tips OR Hints

* Choose 1: Division or inverse (reciprocal), implement 1 and then
build the other by calling division or inverse.
* Never forget the implied bit in the mantissa. Remember to include
the implied bit when you operate on mantissas do it.
* Do not worry about rounding. That’s for 3rd years 😉
* Need a little more help? Wikipedia is always there: https://en.wikipedia.org/wiki/Floating-point_arithmetic
* If you use doubles then you can test against them later.
* If you don’t use floats or doubles you have solved question 5 already 🙂

### Marking for Q2

– [ ] **2 Points** All question2_test passes — no failed assertions
– If and only if all tests pass the following marks are available:
– [ ] **1 Point** All tests pass with -m32 (32bit mode) and -m64 (64bit mode)
– [ ] **1 Point** All tests pass with clang
– [ ] **2 Points** `myfloat_ops.c` and `myfloat_ops.h` lint properly and meet all other quality standards, listed above.

Remember to upload to eclass after making any progress on Q2.
You can upload as many times as you want before the deadline.

## Question 3: String input and output of floating point numbers represented by MyFloat and to be represented by MyFloat.

No one wants to type in the mantissa! No one wants to type binary.
Please compose 2 methods to convert your MyFloats into strings and
back again. You can rely on C’s support for doubles if you want to.
Implement string conversion support in `myfloat_conv.h` and
`myfloat_conv.c`.

We will only require that you parse and produce scientific notation
with lower-case `e`. Scientific notation is shown on the left:

“`
-1.0e-2 == -0.01
1.0e-2 == 0.01
1.0e-1 == 0.1
1.0e0 == 1
1.0000000000000000e0 == 1
1.0e1 == 10
1.0e2 == 100
1.00000000000e2 == 100
“`

The only exceptions are `0`, `-0`, and `inf` and `-inf` for positive
and negative infinite.

`str_to_myfloat` will convert a string into a MyFloat.

`myfloat_to_str` will convert your MyFloat into a string.

1. Read `myfloat_conv.h`.
1. Read `myfloat_conv.c`.
1. Write your name, ccid, and student number into `myfloat_conv.h`.
1. Write your name, ccid, and student number into `myfloat_conv.c`.
1. Modify the `makefile` to compile `question3_test` correctly.
1. Implement `str_to_myfloat()`
1. Implement `myfloat_to_str()`

You should pass:
“`
make clean run-question3_test
make CC=clang clean run-question3_test
make CC=clang ARCH=-m32 clean run-question3_test
make lint-myfloat.c lint-myfloat_ops.c lint-myfloat_conv.c
make valgrind-question3_test
“`

# Hints

* Consider using doubles and sscanf and sprintf.

### Marking for Q3

– [ ] **2 Points** All question3_test passes — no failed assertions
– If and only if all tests pass the following marks are available:
– [ ] **1 Point** All tests pass with -m32 (32bit mode) and -m64 (64bit mode)
– [ ] **1 Point** All tests pass with clang
– [ ] **2 Points** `myfloat_conv.c` and `myfloat_conv.h` lint properly and meet all other quality standards, listed above.
– [ ] **1 Point** Valgrind reports no leaks

Remember to upload to eclass after making any progress on Q3.
You can upload as many times as you want before the deadline.

## Question 4: Binary floating point operations for MyFloat such as addition, subtraction, multiplication, and division without the help of any floating point routines.

Redo question 2 but this time make sure you never use a double or a
float that C provides. You must do it all bitwise (or bitfielded) and
by hand.

No floating point addition, multiplication, addition, or any such
operator. No `math.h` floating point functions either.

You may use signed and unsigned integer operations.

You should be able to use `myfloat_ops.h` and substitute
`purefloat_ops.c` instead of `myfloat_ops.c`.

1. Read `purefloat_ops.c`.
1. Read `question4_test.c`.
1. Write your name, ccid, and student number into `purefloat_ops.c`.
1. Modify the `makefile` to compile `question4_test` correctly.
1. Implement `myfloat_add()`
1. Implement `myfloat_sub()`
1. Implement `myfloat_mul()`
1. Implement `myfloat_inv()`
1. Implement `myfloat_div()`

You should pass:
“`
make clean run-question4_test
make CC=clang clean run-question4_test
make CC=clang ARCH=-m32 clean run-question4_test
make lint-myfloat.c lint-purefloat_ops.c
make valgrind-question4_test
“`

### Pro-tips OR Hints

* Choose 1: Division or inverse (reciprocal), implement 1 and then
build the other by calling division or inverse.

* Consider using 64 bit integers to do your work for you.

### Marking for Q4

– [ ] **3 Point** All `question4_test` tests pass — no failed assertions
– If and only if all tests pass the following marks are available:
– [ ] **1 Point** All tests pass with -m32 (32bit mode) and -m64 (64bit mode)
– [ ] **1 Point** All tests pass with clang
– [ ] **2 Points** `purefloat_ops.c` and `purefloat_ops.h` lint properly and meet all other quality standards, listed above.
– [ ] **-7 Points** C’s own floating point operations and literals are used within `purefloat_ops.c` and `purefloat_ops.h`.

Remember to upload to eclass after making any progress on Q4.
You can upload as many times as you want before the deadline.

## Question 5: That sweet sweet makefile

Who doesn’t love makefiles.

Your makefile must achieve the following:

1. `make clean` must remove all *.o and executable objects you normally compile without error.
1. `make tests` must run all the tests to answer the questions.
1. `make question1_test` must build question1_test successfully with objects, not just .c files.
1. `make question2_test` must build question1_test successfully with objects, not just .c files.
1. `make question3_test` must build question1_test successfully with objects, not just .c files.
1. `make question4_test` must build question4_test successfully with `purefloat.o` .
1. `make lints` must successfully lint `myfloat.c`, `myfloat_ops.c` `myfloat_conv.c`, `purefloat.c`, `purefloat_ops.c`, and `purefloat_conv.c` .
1. `make CC=clang` must compile with clang
1. `make ARCH=-m32` must compile 32 bit binaries

### Marking for Q5

– [ ] **1 Point** Makefile properly builds `question1_test` from object files.
– [ ] **1 Point** Makefile properly builds `question2_test` from object files.
– [ ] **1 Point** Makefile properly builds `question3_test` from object files.
– [ ] **1 Point** Makefile properly builds `question4_test` from object files AND must be built with `purefloat_ops.o` and not `myfloat_ops.o`