Problem with Scientific notification

S

swapna.munukoti

Hi all,

I am new to c#.
I am facing a problem with exponential or scientific notification.
When I use math.pow() funciton, if the numbers are big, it returns me
in exponential format.
And when I do any arthimatic operations on that, it gives me wrong
results.
For example:

?System.Math.Pow(2,60)
1.152921504606847E+18
?System.Math.Pow(2,60)-1
1.152921504606847E+18

How can I solve this problem?
And is there a way to supress exponential notation?

Thanks in advance,
Swapna.
 
J

Jon Skeet [C# MVP]

I am new to c#.
I am facing a problem with exponential or scientific notification.
When I use math.pow() funciton, if the numbers are big, it returns me
in exponential format.

No, it just returns you a double. The double itself doesn't specify a
textual representation - you need to specify your format when you call
ToString if you want a particular one.
And when I do any arthimatic operations on that, it gives me wrong
results.
For example:

?System.Math.Pow(2,60)
1.152921504606847E+18
?System.Math.Pow(2,60)-1
1.152921504606847E+18

What do you believe to be the problem here? You've gone way beyond the
bounds of double's accurate representation - how did you expect the
results to be differentiated?
How can I solve this problem?

Well, you shouldn't expect double to have more accuracy than it
actually does. If you need accurate representations of numbers this
large, you should probably be using a custom maths library. There are
some 3rd party ones around.
And is there a way to supress exponential notation?

Use the appropriate format specifier.

Jon
 
C

colin

Hi all,

I am new to c#.
I am facing a problem with exponential or scientific notification.
When I use math.pow() funciton, if the numbers are big, it returns me
in exponential format.
And when I do any arthimatic operations on that, it gives me wrong
results.
For example:

?System.Math.Pow(2,60)
1.152921504606847E+18
?System.Math.Pow(2,60)-1
1.152921504606847E+18

How can I solve this problem?
And is there a way to supress exponential notation?

Thanks in advance,
Swapna.

you need 60 bits to represent that number, the maths library uses double
precision floating point
to do Pow, wich although I think is about 64 bits some of those bits are
used for the exponent leaving you less than 60 bits.
I think theres an 80 bit float but im not sure if this is in c#

if you want to keep it to a 64 bit integer youl have to write your own maths
routine I think.

I have written some basic math routines myself to replace floating point
libraries in
some small embeded microcntroller wich took too long as it doesnt have a fp
math unit,
but only for things like sqrt,atan2 etc,
however there is quite a bit of code about for such things.

a simple way to do the above would be Int64 x=(Int64)1<<60;

Colin =^.^=
 
S

swapna.munukoti

Thanks a lot for your suggestions.
I will try doing the way you suggested.

Regards,
Swapna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top