Solved

C++ Scientific Notation

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

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 32

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 32

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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