-3

I try to convert this math formula in C++ expression

Expression

But I'm doing something wrong

(log(1.0+a*(getpixel(j,k)))/log10( y ))/(log(2.0)/log10( y ))
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 4
    A general tip for doing complex expressions: Split them up into smaller parts! Also, just saying "something's wrong" is a very poor problem description. *How* is it wrong? Do you get build errors? Do you get run-time errors (a.k.a. crashes)? Do you get unexpected results? – Some programmer dude Nov 20 '15 at 09:06
  • 1
    `a` is not a function (i guess) and you have to write `a* ...` instead of `a(...)`. Also I do not understand why you cast to `int`. – 463035818_is_not_an_ai Nov 20 '15 at 09:07
  • ln(2) is 0.6... as `int` this is 0, so most likely you have a division by zero – 463035818_is_not_an_ai Nov 20 '15 at 09:10
  • I'm not sure if I converted correctly the expresion – Porcescu Gheorghii Nov 20 '15 at 09:12
  • 4
    come on, dont change your question by fixing things you were asking for. You should add the problem description and if you found a solution please post it as answer. – 463035818_is_not_an_ai Nov 20 '15 at 09:12
  • @tobi303 I want to know if that expression was converted correctly – Porcescu Gheorghii Nov 20 '15 at 09:14
  • 1
    You're also not calculating logₑ{...} correctly. `log(...)/log10(y)` is not correct. It looks like you're trying to do the change of base, but you're doing it wrong, and you don't need to do it anyway, since the `log()` function already gives you log base e. To do the change of base you have to use the same base on the top and bottom, and the argument to the bottom is the new base. Using `log` on top and `log10` on the bottom won't work, and you want log base e, not log base y. – bames53 Nov 20 '15 at 09:18
  • Which expression ? The one you posted first ? The one that is there now? The one you will put with the next edit? Atm it is obvioulsy wrong as there is some `log10(y)` that does not appear in the formula. – 463035818_is_not_an_ai Nov 20 '15 at 09:19
  • @bames53 (log(1.0+a*(getpixel(j,k))))/(log(2.0)) something like that – Porcescu Gheorghii Nov 20 '15 at 09:20
  • @PorcescuGheorghii Yes. – bames53 Nov 20 '15 at 09:23

1 Answers1

0

First, the log function already computes the logarithm to the base 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 (e.g. store the a*F, and 1 + a*F separately too).

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);
Andrew
  • 5,212
  • 1
  • 22
  • 40