LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

How would I make a Function->SB Code function?

Root / Programming Questions / [.]

TheV360Created:
I am making V360Calc, a SmileBASIC graphing calculator. The only thing not finished is the function editor, which lets you type a function in. (stored as a string) It then converts the function to SmileBASIC code stored in slot 1. (most of the time) The only problem? I can't figure out how to convert a mathematical function into SmileBASIC code. How would I do this? People that answer will be put on the About dialog and on the page and Miiverse post. Also, small side question: what are the functions that need to be on the keyboard? Currently, I just have all the common ones (π, sine, ^, etc) Final note: if this is impossible, I have an "advanced editor" (directly edit slot 1) that works well. edit: inb4 "don't make a graphing calculator" also this edit 2: Main question is solved, but feel free to suggest things for V360Calc!

Function that I use that you didn't mention directly: log base x, log base 10, ln, x^2 (good for lazy people like me), e,  i, and sqrt. As for your problem, I have a rough idea how to go about this, but I'm sure somebody else on here will code something before I get to it. Plus, they'll do a better job than me. On a side note, if you can figure out how to get your program to do calculus, then you could do some cool stuff. You could find the max and min of functions, which would be handy in defining the viewing screen. You could also calculate the area under a curve, or between two functions.

I think you'll make your life much easier if your function editor uses smilebasic. That way you won't have to parse the string that's entered, and can save it as a callable function directly. The annoying thing about that method is that a typo in a function will cause an error and exit your program. I would: Display help text telling the user that their function can use the variable X, eg X*X or SIN(X). Take the string provided and write it to a program slot. Prefix the string with FUNCTION BLAH(X) RETURN and write the end function line afterwards. Then you can call any user function, passing in a value for X and receiving the value for Y. HTH

(It's DEF, not FUNCTION)
COMMON DEF BLAH(X)
RETURN ...
END
and do USE 1 after editing

How embarrassing! You are, of course, absolutely right. (In my defence I haven't used SB for months:-))

Except that's exactly what I did... I might just make a custom keyboard and call it good to go. I will try to add the calculus functions, though.

Ah I see. Yes a custom keyboard would be good and helps minimise typos. What exactly did you want the user to enter to define a function? Did you want to create an interface that lets the user define a function using proper mathematical notation, pressing custom buttons on the touch screen and seeing the equation represented graphically on the top screen? It's looking very good so far btw!

Probably not in real time, but yeah, that's the idea. Also, I'm probably going to move window settings into the options menu to make way for the math menu.

Create some kind of subroutine which can determine the order of operations within your function, create an array or something which stores the order-of-operations data, and execute the function mathematically, chronologically and individually using a single result variable among the different operations.

You should use lex to check the syntax. I think actorbug implements that program (check the japanese wiki) For the parser you should see lowerdash, n#, Smilebasic custom compilar or any program that compile code. Also, search yacc in google... that is an OpenSource program to generate parsers

New problem! I've been trying to get this function working for a while. It's a function that does something that sounds very simple. It converts (x^2)/2 into ERR_DIV(POW(x,2),2). However, I can't seem to get it working and it keeps spitting out stuff like (ERR_DIV(POW(x,2)),2). I've tried to rewrite the function 3 times and made multiple different methods and even drew a few flowcharts. If someone could give me example code or an algorithm that I've never heard of, that would be amazing. This is one of the last few things I need to fix before V360Calc's first release.