Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to convert a real number into floating point representation without using the internal float type or any C libraries other than the

Write a program to convert a real number into floating point representation without using the internal "float" type or any C libraries other than the one explicitly allowed.

Here is the detail. First download the included file atofStart.zip. Unzip it, this creates a folder with all the necessary files.

Without using the "float" or "double" types (not even casting!) at all in atof.c, implement the function atof.

These are the restrictions of how you can implement atof:

No floating point number operations at all (double or float).

You cannot call functions that you do not write, direct or indirect!

For this assignment, the base-10 exponent can be limited to the range of -5 to 5 (inclusively).

For this assignment, you may assume the base-10 mantissa is from -10 to 10 (exclusively).

For this assignment, you may assume the base-10 mantissa only has up to 4 decimal digits to the right of the decimal point.

You can use any integer type and integer operations. You probably need to use some bitwise operators.

You can declare additional local variables.

You can write and call you own additional subroutines.

You may assume the string is a valid floating point number in base-10 in the following format:

image text in transcribed

[ ] denotes optional

| separates alternatives

+ (without underline) at least one

* (without underline) any number

d a digit 0-9

e the letter e

+ the operator +

- the operator -

. the period

One way to get started

First, if you are doing this in CodeBlocks, your project needs to include both main.c and atof.c! main.c provides a test framework so you don't have to come up with your own framework.

Parse the input string into the following components:

sign: in absence of an explicit sign, it is +,

mantissa: represent the mantissa as a fraction. For example, if the mantissa is 9.62, represent the numerator 962 and denominator 100. The denominator has to do with the presence and position of the decimal point.

exponent: represent the exponent (in base 10) as a different number.

For example, consider the input -1.25e-1. The first step is to break this up to the following:

A -ve sign, sign bit is a 1.

The mantissa is 1.25, represented by numerator in base 10 125 and denominator in base 10 as 100.

The exponent in base 10 is -1.

If you choose to divide this code into subroutines, it is helpful to use a structure to keep track of everything:

struct Base10Parse

{

const char *ptr; // ptr to the last char processed

int sign; // non-zero for -ve

uint64_t num10; // base-10 numerator

uint64_t den10; // base-10 denominator

uint32_t exp10; // base-10 exponent

};

// main.c file

#include #include "ufi.h"

int main() { union UFI u,v;

u.i = atof("1.023e3"); printf("u is %f ",u.f); v.i = atof("1.253e-1"); printf("v is %f ",v.f); printf("u*v is %f ",(v.f*u.f)); return 0; }

// atof.c file

#include "ufi.h"

int64_t atof(const char *ptr) { int64_t x; // do some coding here return x; }

// ufi.h file

#include

union UFI { double f; int64_t i; };

int64_t atof(const char *);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

Question Can any type of stock or securities be used in an ESOP?

Answered: 1 week ago

Question

Question Can a self-employed person adopt a money purchase plan?

Answered: 1 week ago