Technical Forum

STEP 7 / STEP 7 Lite

X raised to power y, Help required.

Thread Starter: sofia   Started: 4/27/2012 7:46 PM   Replies: 11

« Previous thread Next thread »
12» Page 1 of 2 (12 items)
  4/27/2012, 7:46 PM
Joined 6/17/2008
Last visit: 2/5/2013
Posts: 13
Rating:
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Dear experts:
i want to raise x to the power y (x^y). i have written the program but when i calculate 2^21, program shows the result=2097153, but its wrong, it should be 2097152. please help
My code is as under.

      L     2.000000e+000
      LN    
      L     2.100000e+001
      *R    
      EXP   
      RND   
      T     MD    10

Top
  4/27/2012, 8:39 PM
Joined 4/22/2010
Last visit: 5/25/2013
Posts: 3216
Rating:
Rated: Excellent [4.11 out of 5 / rated 295 time(s)]. (295) 
Users with special expertise supervising conferences
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Hello,

That's the result off the round function, Try RND-

links to some calculating stuff
How can you convert a number value of the data type INT into a string in S5TIME, or into the data types BCD and REAL?
and
How do you calculate power, exponential, root, polynomial, logarithm and linear values and how do you convert trigonometric functions into degrees?

Regards,

Marcjan

Problem solved?
Please let us know!

Please no technical questions by PM, use the forum!
Top
  4/27/2012, 8:45 PM
Joined 4/10/2006
Last visit: 5/21/2013
Posts: 224
Rating:
Rated: Outstanding [3.48 out of 5 / rated 31 time(s)]. (31) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
You have run into a limitation of the single precision floating point numbers used in Step7.  They are only accurate to 6 decimal places, and you're trying to get 7.

If you just want a power of 2, you can use a DINT set to 1 and shift it left as many places as the power you want.

HTH,
SteveA
Top
  4/27/2012, 9:10 PM
Joined 3/18/2008
Last visit: 5/14/2013
Posts: 1058
Rating:
Rated: Outstanding [3.96 out of 5 / rated 175 time(s)]. (175) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
sofia wrote:
Dear experts:
i want to raise x to the power y (x^y). i have written the program but when i calculate 2^21, program shows the result=2097153, but its wrong, it should be 2097152. please help
My code is as under.

      L     2.000000e+000
      LN    
      L     2.100000e+001
      *R    
      EXP   
      RND   
      T     MD    10



Dear Sofia,

Please find herewith attched library Block called POW FC1.

Just copy this block to your  program folder and then used it as many times as you want.

Hope this will helps.

Attachment: Library.zip  (21 Downloads)



=== Edited by Amit_86 @ 4/27/2012 9:11 PM [GMT ] ===



Top
  4/28/2012, 8:33 AM
Joined 6/17/2008
Last visit: 2/5/2013
Posts: 13
Rating:
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Dear Amit:
I have seen the math library functions, they give correct results. But i want to know what is the bug in my program.
Regards:
Top
  4/28/2012, 8:41 AM
Joined 6/17/2008
Last visit: 2/5/2013
Posts: 13
Rating:
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Dear Marcjan:
I have tried RND- function but the problem is not solved. Can you fix the bug in my program and suggest some change in my code.
Regards:
Top
  4/28/2012, 9:49 AM
Joined 10/31/2005
Last visit: 5/24/2013
Posts: 2092
Rating:
Rated: Excellent [4.08 out of 5 / rated 200 time(s)]. (200) 
Users with special expertise supervising conferences
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 

Dear sofia, it's impossible - you have no bug in your programblink.

The reason why result is not correct - 32bit REAL have pre-defined precision, in your example you just exceed it's limits and thats why you get wrong result.

2 ways you can solve it - using another method for calculating or using 64bit REAL (But Siemens don't provide it). I did it myself, but i'm not sure i can post it - can discuss it in PM.

Top
  4/28/2012, 10:20 AM
Joined 7/18/2011
Last visit: 5/24/2013
Posts: 278
Rating:
Rated: Outstanding [4 out of 5 / rated 31 time(s)]. (31) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Hi sofia,

Another option is to use modules from the OSCAT library.

www.oscat.de

Kind regards,
BV
Top
  4/30/2012, 1:24 PM
Joined 1/3/2006
Last visit: 5/7/2013
Posts: 545
Rating:
Rated: Outstanding [3.83 out of 5 / rated 29 time(s)]. (29) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Hi there Sofia
As such there is no bug in the problem and this happens only because of the convetion used in PLC to represent floating point. You will notice that your program will work for smaller number. And Step-7 help itself give caution to user which says "Calculations involving a long series of values including very large and very small numbers can produce inaccurate results"

Following point is take from Step-7 help

"The floating-point numbers in STEP 7 are accurate to 6 decimal places. You can therefore only specify a maximum of 6 decimal places when entering floating-point constants"

Note

The calculation accuracy of 6 decimal places means, for example, that the addition of number1 + number2 = number1 if number1 is greater than number2 * 10 to the power of y, where y>6:

100 000 000 + 1 = 100 000 000."


Regards,
Manmeet




Top
  5/1/2012, 2:34 PM
Joined 1/3/2006
Last visit: 5/7/2013
Posts: 545
Rating:
Rated: Outstanding [3.83 out of 5 / rated 29 time(s)]. (29) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Or you can use something like this:




Attachment: pwr.pdf  (8 Downloads)



=== Edited by manmeet @ 5/1/2012 2:40 PM [GMT ] ===
I have no idea why code is appearing so bad, so I have attached it in pdf file

=== Edited by manmeet @ 5/1/2012 2:37 PM [GMT ] ===


=== Edited by manmeet @ 5/1/2012 2:36 PM [GMT ] ===


=== Edited by manmeet @ 5/1/2012 2:35 PM [GMT ] ===



Top
12» Page 1 of 2 (12 items)
Actions