C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 4th, 2008, 10:00 PM
Jay Tee
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Strange Variable assignment - need translation

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)


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



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

Reply With Quote
  #2  
Old July 6th, 2008, 10:27 PM
JohnFrank JohnFrank is offline
Contributing User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Lilyfield NSW Australia
Posts: 30 JohnFrank User rank is Just a Lowly Private (1 - 20 Reputation Level)  
Time spent in forums: 5 h 15 m 55 sec
Reputation Power: 1
Send a message via MSN to JohnFrank
Quote:
Originally Posted by 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)


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



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
You seem to have it correct, slightly changed:
temp = (x = xx - 1.0) + 5.5;
x = xx - 1.0
temp = (xx - 1.0) + 5.5

temp = (x + 0.5) * log(temp) - temp;

The thing that may be getting you depending ont he original code is any implicit casting going on between the variables temp, x and xx. Make sure nothing is getting cast to anything less than a float and the log will make the value useless if you are losing precision somewhere in the conversion.

If you can test it with a manual calulation break down each calculation step to one pair and display the results to check everything matches what you expect it to be.

Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > Strange Variable assignment - need translation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT