Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
mackncheesiest
15
Years of Service
User Offline
Joined: 8th Sep 2010
Location:
Posted: 21st Sep 2010 04:07
I'm trying to make a program to let the person put in a function (i.e. x^2) and then the domain of x values to calculate (i.e. 1 through 10; and I'll have them define what interval to go by) and have the program calculate the answers for the x values from 1 to 10 or whatever they put in. I know it's going to have a for-next loop with the calculating part, but I just can't figure out how to actually put the values in for x since they input the function as a string. Here's all the code I have so far: (btw, this is really early on; I've been working on it for about 10 minutes, and I'm not the sharpest tool in the dbpro box if you know what I mean haha)
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 21st Sep 2010 16:01
To get the numeric value of a string use "x=val(String$)".... Can't be sure if the syntax is correct, but I'm pretty sure it's "val".

The fastest code is the code never written.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 21st Sep 2010 18:40
@ mackncheesiest:

I'm not a math guy but it looks like you want (using the example) x to be a number between the xmin and xmax to the power of countby#. If you calculate it with the variables why ask the user to enter the formula in a string? Also you shouldn't use function$ as your input string since Darkbasic Pro reserves that word for function creation. You may have noticed the editor colors "function" blue... that's why it's blue.

@ Hawkblood:

You got it right.

Dia
21
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 21st Sep 2010 18:41 Edited at: 21st Sep 2010 18:47
maybe I am reading too much into this, but the idea grabbed me just as an interesting challenge.

you will need to parse the input string to determine the constants, variables and operators - cut the input string into several parts so that you can allocate the numbers and functions in your code, as well as your 'x' variable.

i.e. find out where the operators are (operator is ^ + - * / etc) - check out the TEXT command menu commands for help, then use those positions to 'cut' your string

with maths formulae it is long winded but not too complex, although you DO need to build in the basic interpretation rules: Brackets, then indices, multiplication, division, addition, subtraction (I think that's the priority order they go in?)

I have cheated a hell of a lot with this code, (i.e. I have only included the operators I already know I need just for this one example. to expand it to deal with general geometric, trigonometric or plain wierd functions you will need to build them in using IF's or CASE statements) but it should be enough to show you ONE WAY of doing it (it is late so I don't doubt other people can see a better way to do it ) I also only concentrated on parsing the input string, getting user input for the xmin, xmax and xstep values are relatively simple.



the way it is written the user needs to input y=ax^b+c, and a,b and c need to be entered even if they are zero i.e. y=x^2 needs to be entered as y=1x^2+0

if you want to take the time you can put the extra effort into eliminating the need for it, but this should at least give you a starting point

if you want to include SIN for example, you could tell your code to look for the text "SIN" and assign that as an operator

This is not the Sig you are looking for....
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 21st Sep 2010 20:19
Hiya,

If you're also looking for speed, then this plugin might offer you what you need. It's a plugin that parses
mathematical expressions like yours fast.

I have a version here that also supports multiple parsers. But there weren't that many people interested so I never bothered to post it.

Cheers!
Sven B

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Sep 2010 20:48
The only problem I have with that MuParse is that it doesn't support stuff like operator overloading. It would be nice if I could pass in all the basic rules for, say, 3d vector math, and then pass in a string to multiply everything out. It looks like that μParser works by optimizing the equation you pass in... which would be hard to do with operator overloading...

I think there's a need for a good parser that can turn any of these math-related strings or symbols into a series of basic algebraic operations, which then might be in turn optimized with something like μParser to get actual results.

mackncheesiest
15
Years of Service
User Offline
Joined: 8th Sep 2010
Location:
Posted: 22nd Sep 2010 00:55
Thanks for the help everyone; I think I'll be able to get it working how I want from this, but we'll just see how that goes.
Dia
21
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 22nd Sep 2010 14:57
@sven

well if you were looking for the EASY way to do it, then sure you *could* use that set of libraries

*hangs head*

ah well at least it kept me out of the pub for an hour or so while I put that long winded drivel together

This is not the Sig you are looking for....
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 22nd Sep 2010 22:54
Lol Dia,

Even though I made that plugin, I still encourage people to make their own parser. It's a very good coding exercise.
But you have to agree with me that DBP doesn't really provide the tools for speed optimization ^^.

Quote: "I think there's a need for a good parser that can turn any of these math-related strings or symbols into a series of basic algebraic operations, which then might be in turn optimized with something like μParser to get actual results."


I have to agree with you on this one. But the more stuff they put in these things, the slower it tends to be (which is kind of obvious ).

Cheers!
Sven B

mackncheesiest
15
Years of Service
User Offline
Joined: 8th Sep 2010
Location:
Posted: 24th Sep 2010 02:22
Wait, I just realized something: I don't know how to actually add the commands and such from MuParse to the library of darkbasic commands. How do I put install/apply the them since they're not .dll files?
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 24th Sep 2010 17:24
Hi mackncheesiest,

Just putting the DLL file in your plugin-users directoy would be enough. DBP automatically searches the entire folder and picks the right DLL when compiling.

Usually, you just have to copy/paste the folders (compiler/editor/(help)) into your DBP folder, and accept to merge folders.

Cheers!
Sven B

mackncheesiest
15
Years of Service
User Offline
Joined: 8th Sep 2010
Location:
Posted: 25th Sep 2010 09:17
But the file I downloaded has the file extension of ".gz", not ".DLL". Is that how it's supposed to be?
Dia
21
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 26th Sep 2010 04:16
.gz is the file extension for a GZIP compressed file.

you need to uncompress it and the library file(s) will be packed away inside

This is not the Sig you are looking for....
mackncheesiest
15
Years of Service
User Offline
Joined: 8th Sep 2010
Location:
Posted: 28th Sep 2010 07:11
Oh, alright, thanks. I'd just never encountered a .gz file before, and I'd been meaning to get one of the programs to extract those odd zipped files, but I just never got around to it.

Login to post a reply

Server time is: 2026-07-24 23:12:46
Your offset time is: 2026-07-24 23:12:46