简体   繁体   English

python 上的脂肪公式

[英]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? 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计算器链接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 mathematicians 1 default to using base 10, and calling base e logarithms ln .正如我在评论中建议的那样,问题出现是因为math.log默认使用以 e 为底,而大多数数学家1默认使用以 10 为底,并调用以 e 为底的对数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 .首先,如果您需要以 2 或 10 为底,您应该使用math.log2math.log10 The python 3 docs say: python 3 文档说:

math.log2(x)数学.log2(x)

Return the base-2 logarithm of x.返回 x 的以 2 为底的对数。 This is usually more accurate than log(x, 2)这通常比log(x, 2)更准确

But if you're working in another base, use log and add the base as an arg, such as log 3 (x) = log(x,3) .但是,如果您在另一个基础上工作,请使用log并将基础添加为 arg,例如 log 3 (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. PEP8 建议将代码长度保持在 80 个字符以下。 I've tried to format this code for legibility.我已尝试格式化此代码以提高可读性。 This question explains how to break python code across lines这个问题解释了如何跨行打破 python 代码


1 | 1 | Sometimes you'll see mathematicians/programmers use log to refer to log base 2, usually in the context of programming or algorithms.有时您会看到数学家/程序员使用log来指代 log base 2,通常是在编程或算法的上下文中。 Just something to be aware of!只是需要注意的事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM