Testing math formula

public static double formBegin(double n){

return ((log(n)-1)*pow(2,log(n))+1)/n;

}

public static double form1(double n){

return (log(n/2)+1/n);

}

public static double log(double a){

return Math.log(a)/Math.log(2);

}

public static double pow(double a,double b){

return Math.pow(a,b);

}

public static void main(String args[]){

for (int i=1;i<20;i++){

System.out.println((formBegin(i)== form1(i)) + "\t\t\t" +"form1(i)=" + formBegin(i) + ";" +"form2=" + form1(i));

}

For the code above, I am comparing two math formula which is equivalent. With this configuration, it this giving me some true and some false result, because the double cannot simply hold all the decimal value.

I am interested to know, what does normally people does to test two math formula whether they are the same for given range of number using computer programme?

[945 byte] By [william108a] at [2007-11-27 8:36:33]
# 1

A double has 64 bits so there are at most 2^64 numbers that can be represented by a double. This number is quite large but it is not infinite. Therefore, taking the log or power of a double is at best an approximation. Two different forumulations of some problem are likely to result in small differences in result so one cannot test for equality of results. What matters is is how close the results are.

If you are going to be doing lots of calculations then you need to understand floating point numbers so you should read http://docs.sun.com/source/806-3568/ncg_goldberg.html .

sabre150a at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...
# 2

> ...

> I am interested to know, what does normally people

> does to test two math formula whether they are the

> same for given range of number using computer

> programme?

Use a CAS (Computer Algebra System) like Maple, Mathematica, (both commercial) or Maxima (free/open source).

prometheuzza at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...
# 3

> > ...

> > I am interested to know, what does normally people

> > does to test two math formula whether they are the

> > same for given range of number using computer

> > programme?

>

> Use a CAS (Computer Algebra System) like Maple,

> Mathematica, (both commercial) or Maxima (free/open

> source).

A CAS is my favourite piece of software to play with; I happen to be the

proud owner of a cute little HP50G calculator which has a little CAS

availabe; testing for f(x)-g(x) == 0 works most of the time, except it slips

some domain checks, e.g. its answer is yes for the following exp(log(x))-x == 0.

The best CAS is a piece of paper, a pencil and my old Stegun and Abramowitz;-)

kind regards,

Jos

JosAHa at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...
# 4

> The best CAS is a piece of paper, a pencil and my old

> Stegun and Abramowitz;-)

>

Bought my copy in June 1971. Best book I every bought! Has your copy got

Edited by Milton Abramowitz and Irene A. Segun

on the cover and

Edited by Milton Abramowitz and Irene A. Stegun

on the title page?

sabre150a at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...
# 5

> ...

> The best CAS is a piece of paper, a pencil and my old

> Stegun and Abramowitz;-)

> ...

Just browsed the TOC of that thing: an impressive work!

I even found it at a second hand market place for 5 Euro's. I immediately e-mailed the advertiser to see if s/he still has it.

; )

prometheuzza at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...
# 6

> > The best CAS is a piece of paper, a pencil and my old

> > Stegun and Abramowitz;-)

>

> Bought my copy in June 1971. Best book I every bought! Has your copy got

> Edited by Milton Abramowitz and Irene A. Segun on the cover and

> Edited by Milton Abramowitz and Irene A. Stegun on the title page?

I bought my copy much later because I'm much younger and more beautiful

than you of course ;-) I bought mine in 1976; it's the 9th edition and I

just checked: the typo is gone.

I bet all those numbers in your edition are wrong too :-P

kind regards,

Jos ;-)

JosAHa at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...
# 7

> > ...

> > The best CAS is a piece of paper, a pencil and my

> > old Stegun and Abramowitz;-)

> > ...

>

> Just browsed the TOC of that thing: an impressive work!

> I even found it at a second hand market place for 5 Euro's. I

> immediately e-mailed the advertiser to see if s/he still has it.

> ; )

I sell pencil and paper for as little as 3 Euros per piece ;-)

kind regards,

Jos

JosAHa at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...
# 8

> I bought my copy much later because I'm much younger

> and more beautiful

:-)

> than you of course ;-) I bought mine in 1976; it's

> the 9th edition and I

> just checked: the typo is gone.

My copy seems to have a real funny history - I quote

"This Dover edition, first published in 1965, is an unabridged and unaltered republication of the work originally published by the National Bureau of Standards in 1964.

This fifth Dover printing conforms ... "!

>

> I bet all those numbers in your edition are wrong too

> :-P

I was a bit worried about typos in the content when I first noticed the typo on the cover!

sabre150a at 2007-7-12 20:33:31 > top of Java-index,Java Essentials,Java Programming...