Data representation (part 3)
This lesson contains approximately 20 minutes of video content.
Data representation in the computer cont.
Floating-point number representation
Character representation
Activity: Floating point value computation from bit string
Graded Playground Autograder
Activity Prompt:
Background
In this question, you will define BinaryToDouble
, taking a single std::string
value passed by const reference, and returning a double
. The std::string
will encode the floating point value in the same toy format as introduced in the lecture video, and your implementation of BinaryToDouble
must return the floating point value it encodes. For example, the std::string
below encodes the floating point value 3.5
:
Value (as char): 0 b 0 1 0 1 1 0 0 Index (in std::string): 0 1 2 3 4 5 6 7 8 Float Representation - - Sign Exp. Exp. Man. Man. Man. Man.
Assignment Prompt
Your function must ensure the std::string
passed to BinaryToDouble
follows the format we specify, including the "0b"
as the first two characters. The character in
- index 2 stores the sign bit,
- indices 3-4 store the exponent bits, and
- indices 5-8 store the mantissa bits.
'0'
or '1'
. If the std::string
fails to follow the format specified, your function must throw an std::invalid_argument
exception. The exponent is encoded using a bias, so your final exponent must be calculated as an exponential field - bias
, where bias
is equal to 1
.
Functions to Implement
double BinaryToDouble(std::string bit_string);
// BinaryToDouble(“0b0101100”) = 3.5
// BinaryToDouble(“0b0000000”) = 0.0
// BinaryToDouble(“0b1000000”) = -0.0
#include <iostream>
#include "parse_float.hpp"
int main() {
// std::cout << BinaryToDouble("0b0101100") << std::endl;
}
#ifndef PARSE_FLOAT_HPP
#define PARSE_FLOAT_HPP
#include <string>
double BinaryToDouble(std::string binary_string);
#endif
#include "parse_float.hpp"
Submit is not supported in this site.