-5

Ho to evaluate a scientifc expression (x+3x-4+sin x) by passing different values x to find the output

Please let me know the inbuilt function that can be used in java

  • 2
    A quick Google search for "java sin function" can surely help you out. On SO, you need to provide code to show us what you've already tried. Please learn how to ask a question before posting. – amallard Jan 25 '17 at 02:30
  • that basic idea i have it but looking for something different than what your assumption was. – user2484633 Jan 25 '17 at 02:49
  • if i read a function "x+3x-4+Sin x" as string from input or any function entered by user should be evaluated for various values of X – user2484633 Jan 25 '17 at 02:49
  • Ok, well we are not going to write your program for you. Show us what you have tried so far, in code. This is not the place for "write my code for me" this is a place for questions and answers on "specific" questions – amallard Jan 25 '17 at 02:51

2 Answers2

0

Well I am not going give the whole code to you, but here are some hints:

  1. The best way to eval an expression without any external API would be using running the expression as a javascript code and get the result.
  2. Since you just can't do sin(0) + 6 in javascript, you will have to use RegEx to replace all function name to Math.(function name here) without affecting other function name. Such as sin(0) + asin(0)will be replaced to Math.sin(0) + Math.asin(0).
  3. The changing value of x is very simple, just use RegEx to replace the x to a value without affecting other stuff, like x + exp(1) will be turned to 0 + Math.exp(1)
  4. User can run javascript code with your calculator if using javascript, please be careful not to allow users to do so.

Similar question have been asked before, you might want to take a look about it: Evaluating a math expression given in string form

Dolf
  • 134
  • 9
-1

You’re looking for the sin method present in the Math library.

An example:

Math.sin(25); // Returns ‘sin’ of the value ‘25’
D. Ataro
  • 1,711
  • 17
  • 38
  • if i read a function "x+3x-4+Sin x" as string from input or any function entered by user should be evaluated for various values of X – user2484633 Jan 25 '17 at 02:49
  • @user2484633 You’re going to need to parse them out one by one. Start by converting them into a `char` array. – D. Ataro Jan 25 '17 at 02:50