Connecting Tech Pros Worldwide Forums | Help | Site Map

Strange Variable assignment - need translation

Jay Tee
Guest
 
Posts: n/a
#1: Jul 5 '08
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

Ben Bacarisse
Guest
 
Posts: n/a
#2: Jul 5 '08

re: Strange Variable assignment - need translation


Jay Tee <jtrelfa@gmail.comwrites:
Quote:
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).
Quote:
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.

--
Ben.
Jay Tee
Guest
 
Posts: n/a
#3: Jul 5 '08

re: Strange Variable assignment - need translation


On Jul 4, 11:00*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Quote:
>
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.
>
--
Ben.
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.
Closed Thread