Search code examples
javaformulasexponenttemperature

How to format a complicated formula that includes exponents in Java


Update: Ok, it all works now. I figured out what I was doing wrong. In the formula, rH (relative humidity) is supposed to be in a percentage, i.e. "30%". The required input format for the assignment is percent as a decimal, i.e. ".30". Such a simple fix and I didn't even see it. I kept inputting the .30 without converting it to the whole percentage. So I fixed that with a simple .30 * 100 and there it was. Thank you all for helping me, I don't know if I can do the check mark for all the answers so I just picked one.

I am trying to write a code to calculate the dew point. My code works, but the formula I was using was too simple and I wasn't able to get an accurate answer. I found a more advanced formula, which is quite lengthy, and it includes a few exponents. I have been trying to find out how to properly format it, and so far the only thing I've been able to find is the math.pow method, which I think is too simple for this problem. I could be wrong, of course, since I have only been using java for about two weeks.

The formula in question is:

Tdc = (Tc - (14.55 + 0.114 * Tc) * (1 - (0.01 * RH)) - ((2.5 + 0.007 * Tc) * (1 - (0.01 * RH))) ^ 3 - (15.9 + 0.117 * Tc) * (1 - (0.01 * RH)) ^ 14)

The two exponents are the ^3 and ^4. How would I write that in java? As I said, the program does work, this is a simple issue with formatting the formula. Thanks.

EDIT: The website where I found this formula is http://www.angelfire.com/ok5/orpheus/metcal.html

It is in about the middle of the page. It does show ^14, and not 4. I didn't catch that at first, I will double check to see which one works.


Solution

  • 14 as exponent is highly unusual but seems to be correct according to "Linsley et al. (1989)"

    If you are working with local variables, you should use lowercase names.

    Finally, you could define a temp variable equal to 1 - 0.01 * rh since you use it three times in a row :

    double tc = 23; // just an example
    double rh = 67; // just an example
    double rh2 = 1 - (0.01 * rh);
    double tdc = tc - (14.55 + 0.114 * tc) * rh2 -
            Math.pow((2.5 + 0.007 * tc) * rh2, 3) -
            (15.9 + 0.117 * tc) * Math.pow(rh2, 14);