Fat`s formula on python

I have a formula:

%fat=495/(1.29579-0.35004(log(hips+waist-neck))+0.22100(log(growth)))-450

How did this fat formula on python? I make it:

from math import log


fat = 495 / (1.29579 - 0.35004 * log(self.hips + self.waist - self.neck) + 0.22100 * log(self.user.profile.growth)) - 450

But the meanings are far from the truth. Link on calculator https://www.scientificpsychic.com/fitness/diet-calculator-ru.html

(Note: This issue was solved in comments, I'm just adding an answer to mark the question as solved.)

As I suggested in the comments, the issue comes in because math.log defaults to using base e, while most mathematicians1 default to using base 10, and calling base e logarithms ln. There are two good solutions to work around this. First, if you need base 2 or 10, you should use math.log2 or math.log10. The python 3 docs say:

math.log2(x)

Return the base-2 logarithm of x. This is usually more accurate than log(x, 2)

But if you're working in another base, use log and add the base as an arg, such as log3(x) =log(x,3).

All that being said, this question should be solved as follows:

import math

fat = 495 / (1.29579
           - 0.35004 * math.log10(self.hips + self.waist - self.neck)
           + 0.22100 * math.log10(self.user.profile.growth,10)) - 450

Unrelated: Long lines of code can be fairly hard to read. PEP8 recommends keeping your code under 80 characters long. I've tried to format this code for legibility. This question explains how to break python code across lines


1 | Sometimes you'll see mathematicians/programmers use log to refer to log base 2, usually in the context of programming or algorithms. Just something to be aware of!

Back to Top