please translate this into Hitech C

Status
Not open for further replies.

mana111

Member level 1
Joined
Nov 22, 2005
Messages
32
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,569
please i have this code written in proton picbasic and i want to write it in hitech c
can any body help pleaseeeeeeee


Code:
' Create some variables for use with LN and LOG

		Dim LOG_VALUE as Float
		Dim LOG_POWER as Float
		Dim LOG_TEMP as Float
		Dim LOG_X as Float
		Dim LOG_FACTOR as Float
		Dim LOG_XSQR as Float
		Dim LOG_N as Byte
		Dim LOG_TEMP2 as Byte 

' We can't have ln(1) so we must return a zero if it is
		If LOG_VALUE.Byte0 = 0 Then LOG_VALUE = 0: Return
		
' The difference between LOG_N.BYTE0 and $7E will be the 
' amount of 2^LOG_N's that we want to multiply times ln(2)
		If LOG_VALUE.Byte0 <= $7E Then 
			LOG_N = $7E - LOG_VALUE.Byte0
			LOG_FACTOR = -0.69314718 * LOG_N
		Else 
			LOG_N = LOG_VALUE.Byte0 - $7E
			LOG_FACTOR = 0.69314718 * LOG_N
		Endif
		LOG_VALUE.Byte0 = $7E
		
' Begin the taylor series expansion
' ln(1+LOG_X) = LOG_X - (LOG_X^2/2) + (LOG_X^3/3) -+...
		
		LOG_VALUE = LOG_VALUE - 1
		LOG_X = LOG_VALUE
		LOG_XSQR = LOG_VALUE
		LOG_N = 2
		Repeat
			LOG_XSQR = LOG_XSQR * LOG_X
			LOG_VALUE = LOG_VALUE - (LOG_XSQR / LOG_N)
			LOG_XSQR = LOG_XSQR * LOG_X
			LOG_TEMP2 = (LOG_N + 1)
			LOG_VALUE = LOG_VALUE + (LOG_XSQR / LOG_TEMP2)
			LOG_N = LOG_N + 2
		Until LOG_N > 12
		LOG_VALUE = LOG_VALUE + LOG_FACTOR
		Return
 

Hi, you should precise what is the purpose of your program.
Have you already thought about it ?
 

this is a log routine
i know that there is a log function in hitech picc but i want it this way because it's in detail
my biggest problem is how to write this line in c

LOG_VALUE.Byte0 = 0

thanx alot
 

Your C form of your PIC Basic code should like this:
Code:
float log_value(float LOG_VALUE){
float LOG_POWER;
float LOG_TEMP;
float LOG_X;
float LOG_FACTOR;
float LOG_XSQR;
char  LOG_N;
      
      LOG_VALUE--;
      LOG_X = LOG_VALUE;
      LOG_XSQR = LOG_VALUE;
      
      for (LOG_N = 2; LOG_N <= 12; LOG_N = LOG_N + 2){
         LOG_XSQR  *= LOG_X;
         LOG_VALUE -= LOG_XSQR / LOG_N;
         LOG_XSQR  *= LOG_X;
         LOG_VALUE += LOG_XSQR / (LOG_N + 1);
      }
      
      return LOG_VALUE + LOG_FACTOR; 
}
 

thanx alot but what about these lines
these are the most important for me


' We can't have ln(1) so we must return a zero if it is
If LOG_VALUE.Byte0 = 0 Then LOG_VALUE = 0: Return

' The difference between LOG_N.BYTE0 and $7E will be the
' amount of 2^LOG_N's that we want to multiply times ln(2)
If LOG_VALUE.Byte0 <= $7E Then
LOG_N = $7E - LOG_VALUE.Byte0
LOG_FACTOR = -0.69314718 * LOG_N
Else
LOG_N = LOG_VALUE.Byte0 - $7E
LOG_FACTOR = 0.69314718 * LOG_N
Endif
LOG_VALUE.Byte0 = $7E
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…