Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ Scientific Notation

Posted on 2016-09-30
4
Medium Priority
?
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 500 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 1000 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 35

Assisted Solution

by:sarabande
sarabande earned 500 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 35

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

609 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