C++ output with significant digits

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions C++ output with significant digits
# 1  
Old 01-29-2011
C++ output with significant digits

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

As I stated in a previous thread, my assignment is to create a table of calculated data for the U.S. standard atmosphere, in C++. I've completed this part of the assignment, however I would like to clean it up a bit.

2. Relevant commands, code, scripts, algorithms:
This is my table of data, which is correct:

0 0 288.16 101325 1.255 1 1 1

1 1.00016 281.659 89871.2 1.13883 0.97744 0.907432 0.88696

2 2.00063 275.156 79486.1 1.03103 0.954872 0.821542 0.784467

3 3.00141 268.651 70092.1 0.931197 0.932297 0.74199 0.691755

4 4.00251 262.144 61615.5 0.838902 0.909716 0.668448 0.608098

5 5.00393 255.634 53986.5 0.753749 0.887127 0.600597 0.532806

6 6.00566 249.123 47139.1 0.675349 0.864531 0.538126 0.465227

7 7.0077 242.61 41010.8 0.603324 0.841928 0.480736 0.404745

8 8.01006 236.095 35542.6 0.537309 0.819318 0.428134 0.350778

9 9.01273 229.577 30678.9 0.476949 0.796701 0.380039 0.302777

10 10.0157 223.058 26367.5 0.421903 0.774076 0.336178 0.260227

11 11.019 216.536 22559.3 0.37184 0.751445 0.296287 0.222643

12 12.0226 216.536 19197.4 0.316426 0.751445 0.252133 0.189464

13 13.0266 216.536 16384.8 0.270067 0.751445 0.215193 0.161705

14 14.0308 216.536 13983.6 0.230488 0.751445 0.183656 0.138007

15 15.0354 216.536 11933.6 0.196699 0.751445 0.156733 0.117776

16 16.0403 216.536 10183.7 0.167856 0.751445 0.13375 0.100506

17 17.0455 216.536 8689.97 0.143235 0.751445 0.114131 0.0857634

18 18.051 216.536 7414.96 0.122219 0.751445 0.0973856 0.0731799

19 19.0568 216.536 6326.7 0.104281 0.751445 0.0830928 0.0624396

20 20.063 216.536 5397.89 0.0889721 0.751445 0.0708941 0.053273

21 21.0694 216.536 4605.2 0.0759065 0.751445 0.0604832 0.0454498

22 22.0762 216.536 3928.73 0.0647563 0.751445 0.0515986 0.0387735

23 23.0833 216.536 3351.46 0.0552413 0.751445 0.0440169 0.0330763

24 24.0908 216.536 2858.86 0.047122 0.751445 0.0375474 0.0282148

25 25.0985 219.56 2441.23 0.0396842 0.761936 0.0316209 0.0240931

26 26.1065 222.584 2089.01 0.0334972 0.772431 0.026691 0.020617

27 27.1149 225.609 1791.29 0.0283381 0.782929 0.0225802 0.0176787

28 28.1236 228.635 1539.08 0.0240258 0.79343 0.0191441 0.0151895

29 29.1326 231.662 1324.95 0.020413 0.803935 0.0162653 0.0130762

30 30.1419 234.69 1142.78 0.0173792 0.814443 0.013848 0.0112784

31 31.1516 237.719 987.488 0.0148262 0.824954 0.0118137 0.00974575

32 32.1615 240.749 854.837 0.012673 0.835469 0.010098 0.00843658

33 33.1718 243.78 741.308 0.0108533 0.845987 0.00864805 0.00731614

34 34.1824 246.811 643.961 0.00931226 0.856508 0.00742013 0.0063554

35 35.1933 249.844 560.335 0.0080046 0.867033 0.00637817 0.00553008

36 36.2046 252.878 488.367 0.00689282 0.87756 0.00549229 0.00481981

37 37.2161 255.912 426.323 0.00594577 0.888092 0.00473767 0.00420748

38 38.228 258.948 372.742 0.00513755 0.898626 0.00409367 0.00367868

39 39.2402 261.985 326.392 0.00444656 0.909164 0.00354308 0.00322124

40 40.2527 265.022 286.231 0.00385475 0.919705 0.00307151 0.00282488

41 41.2656 268.061 251.378 0.00334699 0.93025 0.00266693 0.00248091

42 42.2787 271.1 221.083 0.00291063 0.940798 0.00231922 0.00218192

43 43.2922 274.141 194.71 0.00253498 0.951349 0.00201991 0.00192164

44 44.306 277.182 171.716 0.0022111 0.961903 0.00176183 0.00169471

45 45.3201 280.224 151.64 0.00193139 0.972461 0.00153895 0.00149657

46 46.3345 283.268 134.086 0.00168946 0.983022 0.00134618 0.00132333

47 47.3493 286.312 118.715 0.00147988 0.993587 0.00117919 0.00117163


I am used to programming in fortran, and when you use double-precision reals in fortran it will keep all the zeroes allocated to that value... ie

REAL, KIND = 8 :: x
x = 1.23
write(*,*) x

will look like...

1.2300000

However, in c++, it cuts off the trailing zeroes.

For the sake of formatting, how can i force c++ to keep the zeroes, and print them to the screen?


3. The attempts at a solution (include all code and scripts):

I've attempted using the command:
cout<<setprecision(n);

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Iowa State University
Ames, IA usa
Ganesh Rajagopalan
AerE261

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 01-29-2011
Quote:
However, in c++, it cuts off the trailing zeroes.
there's nothing to "cut off", it's not represented in decimal at all until you print it.

Decimal places with printf is easy. to print 3 numbers on a line for example:

Code:
printf("%.5f\t%.5f\t%.5f\n", a, b, c);

Replace '5' with the number of digits of your choice.

Last edited by Corona688; 01-29-2011 at 08:48 PM..
# 3  
Old 01-29-2011
My mistake, I was under the assumption that the program would allocate a certain amount of bits for a double precision real, and unused bits are represented by zeroes.

thanks though! That was a HUGE help!!
# 4  
Old 01-29-2011
No problem.

Floating point numbers actually represent numbers like 1.926254 x 2^16 internally, changing the exponent instead of moving the decimal -- that's what "floating point" means -- making decimal places pretty meaningless until you print it. None of it's kept in base 10 even, all in base 2.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Help with change significant figure to normal figure command

Hi, Below is my input file: Long list of significant figure 1.757E-4 7.51E-3 5.634E-5 . . . Desired output file: 0.0001757 0.00751 0.00005634 . . . (10 Replies)
Discussion started by: perl_beginner
10 Replies

3. Shell Programming and Scripting

Print a number up to last significant digit after decimal point

I want to write/print a number through a shell script up to its last significant digit (LSD) after the decimal point. Say, x=10.00056000000000000 I want to print x as x=10.00056. Note that x can be any thing so I cannot know the position of the LSD always. Thanks. (16 Replies)
Discussion started by: hbar
16 Replies

4. Shell Programming and Scripting

Significant Figures in awk

Hello, I've had a look at the printf modifiers, but so far I haven't had much luck in making it do what I need. I need to make it output with a set number of characters every time: whether it's by adding zeros at the end or by rounding, the output has to have the same number of characters in it,... (2 Replies)
Discussion started by: Leo_Boon
2 Replies

5. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

6. Shell Programming and Scripting

shell arithmetic least significant place

I need help on arithmetic root@server # hour=`date | awk {'print $4'} | cut -d: -f 1`; echo $hour 04 Now I subtract this result by 1 or 01 I get "3" as the answer. I need "03" as the answer, ie last two significant numbers should be there. root@server # hour=`date | awk {'print $4'} | cut... (3 Replies)
Discussion started by: anilcliff
3 Replies

7. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies
Login or Register to Ask a Question