Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Calc problem... already :-(

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-09-2007, 11:55 AM
RandomCoder's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Location: Derbyshire, UK
Posts: 623
Awards Showcase
Beta Tester 
Total Awards: 1
Default Calc problem... already :-(

I'm attempting a go at the calculator competition (see this thread http://www.basic4ppc.com/forum/announcements/914-2nd-competition.html).

In doing so I would like to take what I believe is a rather unique approach, however I am already experiencing problems and would like your help.

The problem is this....

Code:
x = 2 + 3
MsgBox(x)
Works like a charm, giving 5 as the result (can't really make it more simple than that)

But now when I do this instead...
Code:
x = 2 & chr(43) & 3
MsgBox(x)
I get 2+3 instead of the desired mathematical result.

So is there a way of forcing the compiler to treat the expression as an equation rather than a string?

Regards,
RandomCoder
__________________
"Defeat never comes to any man until he admits it."Josephus Daniels
Reply With Quote
  #2 (permalink)  
Old 10-09-2007, 12:07 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,734
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

This thread will help you: http://www.basic4ppc.com/forum/code-samples-tips/394-evaluating-mathematical-expressions.html
Reply With Quote
  #3 (permalink)  
Old 10-09-2007, 12:51 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by RandomCoder View Post
So is there a way of forcing the compiler to treat the expression as an equation rather than a string?
I personally don't like the weak typing of B4PPC but you can get it to work for you.

Code:
x = 2 & Chr(43) & 3 & "*" & 4 ' build a valid expression 
y = 0 + x ' coercion forces evaluation of the string
Msgbox(y) ' success
If you use this then you will need an error handler unless you can parse the string for mathematical correctness first.

EDIT: Or use Isnumber() on the string and not try the coercion if it returns false. This approach supports */+- and ^ but not functions such as abs() etc.

EDIT EDIT: Actually Isnumber() isn't reliable in this instance. I don't know why I thought it would be. Stick to the error handler approach!

Last edited by agraham : 10-09-2007 at 01:21 PM.
Reply With Quote
  #4 (permalink)  
Old 10-09-2007, 01:21 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,734
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

This trick is dangerous.
I recommend you to use the code sample in the link I've posted instead.
It will only work if evaluated once.
This code for example will fail on the second time:
Code:
Sub App_Start
    
For i = 1 To 2
        x = 
"2*5+4" 
        y = 
0 + x 
        
Msgbox(y)
    
Next
End Sub
The reason it fails is that there is an optimization process that optimizes calculations after the first calculation.
Reply With Quote
  #5 (permalink)  
Old 10-09-2007, 01:28 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by Erel View Post
The reason it fails is that there is an optimization process that optimizes calculations after the first calculation.
Pity - I thought that it might be quite useful


EDIT: Actually this probably explains some inconsistent results and inexplicable errors I have encountered trying some fairly complicated string maniipulation where I was (rather less blatently than above) using B4PPCs apparent use of weak typing to reduce the amount of code.

Last edited by agraham : 10-09-2007 at 01:37 PM.
Reply With Quote
  #6 (permalink)  
Old 10-09-2007, 02:24 PM
RandomCoder's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Location: Derbyshire, UK
Posts: 623
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Thanks for the input guys, I had already tried using a variety of amplisand, brackets and single quotes but all to no avail.

With some help from a friend of mine he has got it working using the following method...

Code:
x=(2 Chr(433)
MsgBox(x)
I must admit that I had already tried this without the brackets and all I got was a value of 2, but with the brackets it's evaluated correctly and produces a value of 5.

Thanks for the help.

Regards,
RandomCoder
__________________
"Defeat never comes to any man until he admits it."Josephus Daniels
Reply With Quote
  #7 (permalink)  
Old 10-09-2007, 02:27 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,734
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

This will not be a proper solution too.
It will fail on the second call.
Try using the Eval method that is implemented in this link: http://www.basic4ppc.com/forum/showthread.php?t=394
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Touch style standard calc Alex812 Share Your Creations 2 12-17-2007 01:05 PM
MyRPN - my piece for calc competition Standa Share Your Creations 1 11-04-2007 07:45 PM


All times are GMT. The time now is 11:19 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0