12,508,422 members (56,895 online)
Rate this:
Please Sign up or sign in to vote.
See more:
You have to write a computer program that will add 1/3 to itself a large number of times and to compare the result to multiplying 1/3 by the number of times 1/3 was added to itself. It is also to do the same thing with ½.
The program is to do this arithmetic twice, once using single precision (float) and once using double precision (double). Both of these will be in one program. Make certain you use a type for your counter that works with these large numbers.
Your program will do these additions 109 (1 billion) times. It is to print out the results, every power of ten times (10, 100, 1000, etc. up to 1 billion) along with the result of multiplying 1/3 to itself up to that point. Do NOT use fixed point to output the results. Each output should give the power of 10, the result from the addition of 1/3 to itself up to that point, the multiplication of 1/3 times the number of additions, the difference between these last two numbers, the result of adding ½ to itself up to that point, the multiplication of ½ times the number of additions up to that point, and the difference between these last two numbers. The program must output a header before this data explaining what the output is. Remember to do this for both single and double precision.
Make certain to have comments at the beginning of your program with your name, assignment number 3, date, and a brief explanation of the program. Also make certain to put comments within your program. Make certain to properly block the code in your program. Pay attention to the quality of your output, it should not be slopped all over the screen, but be in neat columns (perhaps use \t in the output statements). Remember you are only to do the multiplications when you output, do NOT do them every pass in the loop.
Items to think about:
1. Did you notice any difference between the results for ½ and 1/3?
2. Did you notice any difference between the results for single and double precision?
Posted 13-Oct-12 18:47pm
Updated 15-Oct-12 11:26am
v2

## 4 solutions

Rate this:
Please Sign up or sign in to vote.

## Solution 1

This sounds like homework.

Unfortunately the folks who donate their time to help you in this forum do not look kindly to people who don't bother or are too lazy to do things for themselves.

Homework is for you to learn.

Please respect the time of others and ask genuine questions.
Rate this:
Please Sign up or sign in to vote.

## Solution 2

It does not work like this here.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.
Rate this:
Please Sign up or sign in to vote.

## Solution 4

As pointed out, we won't do your homework. but that doesn't mean we won't help at all.

Given this wall of text, I can imagine you feel intimdated and have trouble getting started. I suggest you focus on each paragraph individually, make sure you can get the instructions to work in a computer program, and only continue with the following paragraph if you're sure you're done with the previous.

That said, some of the instructions - specifically the first paragraph - are hard to understand. The main goal of this homework is to understand the effect of rounding errors, and the limits of machine precision. I'll reformulate the task - maybe you will have an easier time understanding this:

Summary
Write a program that calculates some values using repeated addition or multiplication, and print the results. Try different operands, and different variable types, and compare the effects of these choices.

Instructions
1.a) Add the value 1/3 for 100 times, using variables of type `float`
1.b) print the result of the addition after 10 and after 100 additions
2. Multiply the value 1/3 by 10, and by 100, using variables of type `float`, and print these results
3.a) Add the value 1/2 for 100 times using `float`
3.b) print the result of the addition after 10 and after 100 additions
4. Multiply 1/2 by 10 and by 100 using `float`, and print that
5.a) Add 1/3 for 1000000000 times, using variables of type `double`
5.b) print the results after 10, 100, 1000, ... , 1000000000 additions
6. Calculate and print the results of multiplying 1/3 by 10, 100, 1000, ... , 1000000000 .
7.a) and b) and 8.: as above, using a base value of 1/2 instead of 1/3

Try to implement each individual step and make sure it works before continuing.
Comments
CPallini 15-Oct-12 6:17am

My 5.
Humberto 15-Oct-12 6:52am

Very instructive. My 5!
Nelek 15-Oct-12 17:39pm

Mine as well.
Rate this:
Please Sign up or sign in to vote.

## Solution 5

This is not a place to do homework. i suggest you to do it for yourself.

try to do it it yourself. i you will face any problem then we will definitely help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 385 Suvendu Shekhar Giri 190 Richard Deeming 175 F-ES Sitecore 150 Vincent Maverick Durano 150
 OriginalGriff 7,891 ppolymorphe 4,062 Maciej Los 3,921 David_Wimbley 3,196 Karthik Bangalore 3,121

Advertise | Privacy | Mobile
Web01 | 2.8.160927.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100