Solved

C++ Scientific Notation

Posted on 2016-09-30
4
43 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

861 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