Strange Variable assignment - need translation

J

Jay Tee

I'm trying to decipher this C code and translate it into PHP (long
story). Can anyone help?

(xx is a float being passed into the function)
--snip--

temp = (x = xx - 1.0) + 5.5;
temp = (x + 0.5) * log(temp) - temp;

--snip--

My assumption was this:
x = xx - 1.0
temp = x + 5.5
then later, temp = (x + 0.5) * log(temp) - temp

For some reason, I don't think my assumption is correct as the wrong
values are coming out of the function. The weird use of parentheses/
assignment operators is throwing me off.

Is there a different (a.k.a. more verbose) way to write that snippet?
I want to make sure I get this right.

Thanks,

Jon
 
B

Ben Bacarisse

Jay Tee said:
I'm trying to decipher this C code and translate it into PHP (long
story). Can anyone help?

(xx is a float being passed into the function)
--snip--

temp = (x = xx - 1.0) + 5.5;
temp = (x + 0.5) * log(temp) - temp;

--snip--

My assumption was this:
x = xx - 1.0
temp = x + 5.5
then later, temp = (x + 0.5) * log(temp) - temp

Looks the same to me (except for the syntax errors from the missing
;s).
For some reason, I don't think my assumption is correct as the wrong
values are coming out of the function. The weird use of parentheses/
assignment operators is throwing me off.

You can write exactly the same in PHP as the original. No need to
spit it up. The fact that it comes out differently suggests that
there is some issue about the *types* of the these variables, rather
than the statements that you've show here. C types behave very
differently to PHP types but you don't tell us what types x, xx and
temp are, or how you know it "comes out" differently.
 
J

Jay Tee

You can write exactly the same in PHP as the original.  No need to
spit it up.  The fact that it comes out differently suggests that
there is some issue about the *types* of the these variables, rather
than the statements that you've show here.  C types behave very
differently to PHP types but you don't tell us what types x, xx and
temp are, or how you know it "comes out" differently.

Thank you :)

I've been wracking my brains for an hour and went ahead and wrote it
the same in PHP (as you suggested) rather than making it more verbose;
turns out I was overthinking things and should have just done it that
way in the first place! Plus I was missing a pair of parens on a
later calculation - probably contributed to my frustration.

On a side note, everything was a float in the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top