Math Question

BlitzMax Forums/BlitzMax Beginners Area/Math Question

Zacho(Posted 2009) [#1]
I have a really simple math question to ask. Is it possible for BMax to Print a number with more than one decimal not being a zero (can you tell BMax to not round and show all digits) and is it possible to have [100%] in an equation.


Here's my code. It is very straightforward coding (If this is true do this, if this is false do this). Please don't criticize that my code isn't coded right or efficient (it does the job that it has to do).

As for the question about the math, under
 'DATA 
, I will list all the elements. The equation that BMax is having trouble with is ((Mass of element*number of that element)/total mass of compound) * 100%. Does "totalmass" need to be a Float var so the equation will work if the mass is 89.23 or 90? Or should it be double?

Thanks in advance guys!




Brucey(Posted 2009) [#2]
That's going to be one verrrrrrrry long program when you're finished :-)


100% of something is itself, which is the same as multiplying by 1, if I'm not mistaken?

If you wanted 10%, you'd multiply by .1


You want to round your answers to how many decimal places?


Good typing practise though, I expect.


Bobysait(Posted 2009) [#3]
I don't really understand the question... but if you expect "float" values while giving "int", you have to cast your equation .

maybe you're looking for something like this

(Float(227*mol1)/totalmass)


Zacho(Posted 2009) [#4]
@Brucey- Yes, I do praise myself on my length of my programs. I have made a periodic table search engine program that is 1,000+ lines (ctrl c + ctrl v =))

About the 100%, I am asking about this because sometimes the answer doesn't match up if I check it with a calculator, I am just making sure that that (the 100%) isn't messing up my code.

I don't actually need the answers to be rounded. I need them to be not rounded. This is because we have to write answers with a certain amount of digits (significant digits) and the place where you round to get the correct number of sig digits varies for each equation. 8 places after the decimal should be plenty.


@Bobysait- Ya, I realized after I posted this up :p. So when asking for input for "totalmass" I would call it like this:
totalmass=Float(Input("..."))



This is for my AP Chem class.


xlsior(Posted 2009) [#5]
Just realize that floats are inherently not accurate, so if you perform math with them the errors may accumulate over time and the results end up less and less accurate the more math function you perform on them.

The only way around that is using a (slower) arbitrary-precision math library. I believe that Brucey has one for download.


Brucey(Posted 2009) [#6]
8 places after the decimal should be plenty.

Use Double instead of Float, although they still are not entirely accurate due to the nature of how the numbers are stored in memory.

Show me an example of what you think a calculation result should be, and what you are getting from your program.


Zacho(Posted 2009) [#7]
Hmmm... Double doesn't seem to help as well. Ok here are the variables: (oxygen is the element, the total mass of the compound is 64.25, and the number of moles in the compound is 2) The equation should look like ((16.01*2)/64.25). My answer with a calculator is 49.84%.
Float: 7.06614780%
Double: 7.0661478599221788%

Just a little off eh?


_Skully(Posted 2009) [#8]
I get:

0.498365760
0.49836575984954834

Local a:Float=((16.01*2)/64.25)
Local b:Double=((16.01*2)/64.25)
Print a
Print b



Brucey(Posted 2009) [#9]
I get what Mulder's friend gets too, surprisingly enough - just short of 50 %.

One would have to consider that your code isn't working :-)


Matty(Posted 2009) [#10]
There must be something wrong with your calculation. An error in your code.


_Skully(Posted 2009) [#11]
The equation should look like ((16.01*2)/64.25)


Maybe the problem is that in your code... it doesn't? Without further code we'll just have to file this under x


Zacho(Posted 2009) [#12]
I think I now know what the problem is, my code is
 ((mass of element * number of element)/ total mass] 
no where in my code do I state that the answer should be in float value. So Im thinking that "totalmass" needs to stay a float and that for each equation I have (Float(_equation_)) right?


Zacho(Posted 2009) [#13]
Yay it works now!! Thanks soo much everyone who posted. Here's my uncomplete but working code:




One last question I have is, can I save some typing by condensing this somehow?


xlsior(Posted 2009) [#14]
One last question I have is, can I save some typing by condensing this somehow?


Yes, tremendously so. One word: Functions.
Rather than spelling out essentially the same formula for each element, you can splitthe entire program up in a few generic functions, and call those instead.
The way you wrote the program, you'll have to spell out the entire periodic table for each and every element to add. Imaging what you'd have to type if you are trying to analyze a mixture with a dozen elements!
Not to mention, if they discover a new element, you would have to add additional checks all over your program.

First, you ask for the name of the element, and then have the weights hardcoded. This is replicated for each individual element. You could do something like this (quick & dirty, not tested, but you'll get the idea)

print "The weight of Aluminum: "+GetWeight("aluminum")

Function GetWeight:Float(Element:String)
	Select Lower(element) ' Convert to lowercase first, so we won't have to doublecheck all permutations
		Case "actinium","ac","89"
			Return 227
		Case "aluminum","aluminium" ' Note: US and UK spellings are different! Need to check for both.
			Return 26.98
		Default
			Return 0
	End Select	

End Function


Using a function like this also means that there's only a single place in your program to teach it about a new element -- for example, if they end up discovering that a specific new element has a slighty different weight after all, or they end up giving a newly discovered element an official new name (as opposed to the placeholder names assigned to the missing/unobserved elements -- for example, unununium got renamed roentgenium in 2004), it's trivial to implement it in your program. Add it to the select statement in the GetWeight function once, all calculations in your programs are now aware of it and can use it.

This way you can have a single function with all your element names/numbers/etc, and by feeding it a name it will give you the result. You can use this in your program in a form like this:

totalmass=Float(Input("What is the total mass of the compound? "))
element1=Input("What is the name of the first element? ")
mol1=Int(Input("How many moles of this element is in the compound? ")) 

answer:Float=((GetWeight(element1)*mol1)/totalmass)*100	



xlsior(Posted 2009) [#15]
Note that you can further generalize this too: instead of using hardcoded variables like element1, element2, element3, element4, you can use an array:

to handle 10 elements, you can declare something like this:

MaxElements:10
global elements:string[maxelements+1]
global weights:float[maxelements+1]
global mols:int[maxelements+1]
global answers:float[maxelements+1]


then you can loop through them like this:
(Not tested, but you'll probably get the idea)

totalmass=Float(Input("What is the total mass of the compound? "))

for temp:int=1 to MaxElements
   elements[temp]=Input("What is the name of element "+temp+"? ")
   mols[temp]=Int(Input("How many moles of this element is in the compound? ")) 
   answers[temp]=((GetWeight(elements[temp])*mols[temp])/totalmass)*100	
next



Brucey(Posted 2009) [#16]
One last question I have is, can I save some typing by condensing this somehow?


Um.... I thought you were mostly happy with your code?
Please don't criticize that my code isn't coded right or efficient (it does the job that it has to do).


I didn't mention any ways to improve it, because you told me not to.


Zacho(Posted 2009) [#17]
@xlsior- Ok. The functions make sense to me. I might convert it to a function-based program (Btw: I didn't use all of the elements, only ~50 that are common). The arrays on the other hand are not familiar to me. (As you may have seen my earlier posts, I am a noob at this stuff).

For the record, I did not add any other conditions to be met for the compiler to recognize H, hydrogen, or 1 as hydrogen. I tried this earlier and it is what was causing the computing errors (dont ask me how).

I may ask you about the functions if I attempt it. And TY for putting in some framework code for me. It helps.

@Brucey- I was just wondering if there was a way to condense this; I AM happy with my code (it works) but I want to save my tedious working fingers.

The reason I posted "Please don't criticize that my code isn't coded right or efficient (it does the job that it has to do)" was because if I didn't post it I might get some "Use Superstrict/Strict/Functions/Arrays/etc." and the truth is I'm not that familiar with it (I have looked @ the tuts.) and it would've taken even longer to explain it to me and implement it with my code.


xlsior(Posted 2009) [#18]
The arrays on the other hand are not familiar to me


Put very simply, an array is a collection of similar variables.

a:int[10] creates an array of integers, with 10 'slots'.
you can address these individually:

a[0],a[1], a[2], a[3], a[4], a[5], a[6],a[7],a[8] and a[9]

the advantage of using an array over single variables like a0, a1, a2, a3, etc. is that you can increment the array variables programmatically:

for temp=0 to 9
a[temp]=temp
next

will result in:
a[0]=0
a[1]=1
a[2]=2
a[3]=3, etc....

without an array you would have to assign each value manually... Plus repeat a lot of code.

you can also store grids and such in multi-dimensional array, but you should probably become familiar with single-dimensional array like mentioned above before you jump into those.


Zacho(Posted 2009) [#19]
So like a:int[10] creates an array of 10 numerical values with the handle a? And you can change each value through "For [temp]=0 to 9 ; a[temp]= temp ; next"
And for that piece of code you are telling blitz when it sees temp in the a array in brackets to apply temp to the a array right? This is applied to all "holders" in a correct?