Solved

C++ Scientific Notation

Posted on 2016-09-30
4
28 Views
Last Modified: 2016-10-24
Using math.h
double value;
value = 1.97* pow(10,-4);

Open in new window


Returns 1.97e-04

I am looking for .000197 to be what is returned and I'm not sure where I'm going wrong.
0
Comment
Question by:Computer Guy
  • 2
4 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 125 total points (awarded by participants)
ID: 41824423
You need to use 'printf' or 'sprintf' to get it to be formatted like that.  Normal math routines will not keep all those leading zeros.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points (awarded by participants)
ID: 41824449
I happen to also like printf or sprintf. But there are C++ purists who do not. If you are one of them, then take a look at this.
http://www.cplusplus.com/reference/ios/scientific/
Following sample code from this link shows how to format according to your needs.
// modify floatfield
#include <iostream>     // std::cout, std::fixed, std::scientific

int main () {
  double a = 3.1415926534;
  double b = 2006.0;
  double c = 1.0e-10;

  std::cout.precision(5);

  std::cout << "default:\n";
  std::cout << a << '\n' << b << '\n' << c << '\n';

  std::cout << '\n';

  std::cout << "fixed:\n" << std::fixed;
  std::cout << a << '\n' << b << '\n' << c << '\n';

  std::cout << '\n';

  std::cout << "scientific:\n" << std::scientific;
  std::cout << a << '\n' << b << '\n' << c << '\n';
  return 0;
}

Open in new window

Possible output:

default:
3.1416
2006
1e-010

fixed:
3.14159
2006.00000
0.00000

scientific:
3.14159e+000
2.00600e+003
1.00000e-010

Open in new window

1
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 125 total points (awarded by participants)
ID: 41829350
like printf or sprintf. But there are C++ purists who do not.

printf or sprintf may not only avoided by purists but also by programmers who want to make their software safer, better readable, and less error-prone.

safer: the format string usual provided as literal could be used by malware to hijack the process and run malware code with the access rights of the process. another risk is that the printf statement can be used for a buffer-overflow attack.

better readable: beside of simple formats those statements are an imposition for anyone who has to maintain such code.

error-prone: the compiler neither can check the format nor the arguments. when using sprintf there is no way to validate that the buffer is big enough.

std::cout << "fixed:\n" << std::fixed;

you may use std::setprecision(n) to get n significant digits (rounded at the n-th digit) if used without std::fixed or to get n digits after the decimal point if used with std::fixed.

Sara
0
 
LVL 33

Expert Comment

by:sarabande
ID: 41856702
As the question was posted in the C++ TA, the comment of phoffric showing c++ code samples is the solution.

However, printf and sprintf are widely used to convert and format Integer and Floating Point numbers also in C++ code. therefore Dave Baldwin's suggestion and my comment may serve as a helpful addition to the solution.

Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now