简体   繁体   English

来自数学公式的 C++ 数学表达式

[英]C++ math expression from math formula

I try to convert this math formula in C++ expression我尝试在 C++ 表达式中转换这个数学公式

表达

But I'm doing something wrong但我做错了什么

(log(1.0+a*(getpixel(j,k)))/log10( y ))/(log(2.0)/log10( y ))

First, the log function already computes the logarithm to the base e .首先, log函数已经计算了以e为底的对数。 You don't need to perform any change-of-base.您不需要执行任何基数更改。

Second, split your expression into parts to make it easier to write, and to understand:其次,将您的表达式分成几部分,以便于编写和理解:

const double F = getpixel(j, k);
const double numerator = log(1.0 + a * F);
const double denominator = log(2.0);
const double result = numerator / denominator;

You could choose to split it more (eg store the a*F , and 1 + a*F separately too).您可以选择更多地拆分它(例如,也将a*F1 + a*F分开存储)。

Once you've got that, if you really want it in a single line, it's easy enough to combine (but there's no need; the compiler will typically merge constant expressions together for you):一旦你得到了它,如果你真的想要在一行中,它很容易组合(但没有必要;编译器通常会为你将常量表达式合并在一起):

const double result = log(1.0 + a * getpixel(j, k) / log(2.0);

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

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