scientific notation

formatting output to be in scientific notation

Page 1 of 1

8 Replies - 20411 Views - Last Post: 22 February 2009 - 01:45 PM Rate Topic: -----

#1 keli_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-January 09

scientific notation

Post icon  Posted 21 February 2009 - 04:38 PM

I don't have a code to post, just a few questions.

How do I format my number to print as scientific notation.

for example

150e6 for 150x10^6.

Also for numbers that large should I be using a long double or a long float?

I would also like to know if there is a way to format all of my font to print out as a specific font ie courier, when I print my results to a file.

One more question when using an array do I have to use the [0][0] position? It would be easier for the program I am writing to have the array start at [1][1] since I am using several matrices, and it is more practical to start at 1 than zero.

thank you so much for answering my questions.

Is This A Good Question/Topic? 0
  • +

Replies To: scientific notation

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: scientific notation

Posted 21 February 2009 - 04:53 PM

If you use a primitive type to handle the number, it probably should be a double to offer the most precision. You would probably have a function to take a double that would return a string that had the scientific format. You would need some decimal movement and double to string translations (if you want us to actually show you how, you'll have to give it a try yourself first and post your efforts :) ). I'm not sure about the font, it may depend on whether the format is plain text, or something more advanced, like rich text.

As to the array issue, C arrays invariably start at [0], and there's nothing we can do about it. I doubt compilers have options for it anywhere, because that would lead to issues with different compilers and the same code. You could make your own class to handle the situation, that subtracted one from each access call for access to an array it contained, but I think that would be little excessive purely to give you different index access. You could just try to subtract one from where ever you want to access an array, but its one of those things that you'll just have to live with.

Hope this helps!
and welcome to Dream In Code!!!
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: scientific notation

Posted 21 February 2009 - 04:57 PM

No need to reinvent the wheel, iomanip has a scientific set: Read here

code example:

// modify basefield
#include <iostream>
using namespace std;

int main () {
  double a,b,c;
  a = 3.1415926534;
  b = 2006.0;
  c = 1.0e-10;
  cout.precision(5);
  cout	   <<		 a << '\t' << b << '\t' << c << endl;
  cout <<   fixed	<< a << '\t' << b << '\t' << c << endl;
  cout << scientific << a << '\t' << b << '\t' << c << endl;
  return 0;
}


Was This Post Helpful? 1
  • +
  • -

#4 keli_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-January 09

Re: scientific notation

Posted 21 February 2009 - 05:38 PM

Thanks

I know there are built in functions to print out and read scientific notation ... just don't know what they are. Your program is in C++ and I am writing in C. My book is missing the pages that show formatting ..(shorter version goes from page 500 to 1100).

I will try to read in the file in scientific because ultimately I need it to print out that way.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#5 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: scientific notation

Posted 22 February 2009 - 06:05 AM

You can use printf
http://www.cplusplus...dio/printf.html

// modify basefield
#include <cstdio>
using namespace std;

int main () {
  double normalValue = 15.32;
  double largeValue = 1.254e25;

  // %e always shows scientific notation
  printf("normalValue: %e \n", normalValue);
  printf("largeValue: %e \n", largeValue);

  // %g shows scientific notation when a value is large
  printf("normalValue: %g \n", normalValue);
  printf("largeValue: %g \n", largeValue);

}


Was This Post Helpful? 1
  • +
  • -

#6 keli_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-January 09

Re: scientific notation

Posted 22 February 2009 - 10:23 AM

thank you.

That was what I needed to know. When I scan it it from file. I would just scan in %e as well?

Also what if it is a very small number should I be putting it in the format of
1.2345e-10 and 1.2345e+6 ?

oh and what is that using namespace standard thing? is that a library function?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: scientific notation

Posted 22 February 2009 - 10:24 AM

That's a C++ thing, you don't need it.
Was This Post Helpful? 0
  • +
  • -

#8 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: scientific notation

Posted 22 February 2009 - 10:33 AM

Well, printf has a lot of options, you can study the page at cplusplus.com I linked to. If you want to display a + or - in the scientific notation you can use printf("%+e", myvalue);, and if you want to specify the number of decimals to for example 5 you can do printf("%+.5e", myvalue);.

As for the std namespace: this is for convenience. For example when you use the string library, you need to define a string like std::string myString = "sometext";, as the string is defined in the namespace std. For convenience, you can define that you want to use the std namespace on top of your code with the line using namespace std;. Then, you can define a string without having to specify the namespace where it is defined: string myString = "sometext";.
Was This Post Helpful? 1
  • +
  • -

#9 keli_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-January 09

Re: scientific notation

Posted 22 February 2009 - 01:45 PM

Thanks again.

I scanned in numbers both ways
as 2.0e5
and 1.5e+8

they both printed out with the plus sign to my file.

This is what I wanted now I will see how they work in calculations ....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1