Hi
It might sound strange but can I simplify it !!
I have only one variable for input which is diameter of a circle in millimeters.
This ultimately converts into a Guage value as in gun barrels. I can do this in Excel but haven't much of a clue about coding for a web page element.

So I have input X halved to give a radius
Then 4/3 pi r cubed gives the vloume in cu mm
Volume x 0.01134 which is the weight in grams of lead per cubic mm gives the weight of the sphere
this is then divided into 453.59237 which is the weight of a lb of lead in grams
result is number of spheres per lb lead i.e. the guage

So if we start with a bore of 23mm (0.906")
we end with a guage of 6.278 and a weight of 72.26gms (2.549 oz)

Help would be really appreciated.

I could not get the math to work in the end but hopefully this is enough of a step by step that you are able to fix the math.

<html>

<head>

<script language="javascript" type="text/javascript">

function calculateTotal(myForm) {
	var dia1 = Number(myForm.diaOfCircle.value);
	
	var PI_Value = ((4/3) * Math.PI) * (dia1 / 2);
	var Cubed_Value = Math.pow(PI_Value,3);
	var Weight_In_Grams = Cubed_Value * 0.01134;
	var Weight_In_Pounds = Weight_In_Grams /453.59237;

	myForm.PI_Value.value = PI_Value;
	myForm.Cubed_Value.value = Cubed_Value;
	myForm.Weight_In_Grams.value = Weight_In_Grams;
	myForm.Weight_In_Pounds.value = Weight_In_Pounds;
}

</script>

</head>

<body>

<form>
	<input type="text" name="diaOfCircle" value="23">Diameter of a circle<br /><br />
    PI Value<input type=text name="PI_Value"><br />
    Cubed Value<input type=text name="Cubed_Value"><br />
    Weight in Grams<input type=text name="Weight_In_Grams"><br />
    Weight In Pounds<input type=text name="Weight_In_Pounds"><br />
    <input name="Calculate" type="button" value="Calculate Guage" onClick="calculateTotal(this.form)">
</form>

</body>

</html>

Ken

Firstly so many thanks for helping out with this one. Once it's cracked I'd like to add a credits line for Daniweb if that would be ok.

Somehow we're ending up with much too large a result at the moment.

The visable fields are as follows - better seen a a graphic)
[IMG]http://flintlockandsteel.co.uk/images/calculator.jpg[/IMG]

1) Variable entry (the diameter) is assumed to be millimeters (if inches - convert to mm in mm box and visa versa - only 1 entry but show both) Use mm box for calcs

2) Convert diameter (mm) of sphere to volume in cubic mm: V= 4/3 * pie(3.142857143) * radius (diameter/2) cubed

3)Multiply the cube value by 0.01134 (this gives the weight of the Lead sphere (musket ball in grams)

Note - For Ounces and grains use: 1000 grains = 64.8 gms = 2.286 oz - show result

4) Divide this value into 435.59237 (this gives the number of spheres per Lb pound which is actually the guage) - Show as result

If we start with input value 18mm we should get a guage of 13.1

Note show Guage to 1 decimal point only .

Hope that's set it out a bit more clearly.

Using the form I created for you and just doing the math with a calculator I can not get 18mm to equal 13.1 gauge. Please send me your excel sheet.
When I do the math I get 1.3394936841956771280014027604205

Ken

This should work for you. You will have to lay it out in the format you want.

<html>
     
    <head>
     
    <script language="javascript" type="text/javascript">
     
    function calculateTotal(myForm) {
		var dia_in_mm = Number(myForm.diaOfCircle_mm.value);
		var dia_in_in = Number(myForm.diaOfCircle_in.value);
		var run_calc = 0;
		
		if ( !!dia_in_mm ) {
			//Convert it to Inches for display only
			dia_in_in = dia_in_mm * 0.0393700787;
			//Display the conversion
			myForm.diaOfCircle_in.value = dia_in_in;
			run_calc = 1;
		}
		
		if ( run_calc == 0) {
			//Check to see if they entered an inch measurement.
			if ( !!dia_in_in){ 
				//Convert it to mm
				dia_in_mm = dia_in_in * 25.4;
				//Display it on the form
				myForm.diaOfCircle_mm.value = dia_in_mm;
				run_calc = 1;
			}
		}
		
		if (run_calc == 1) {
			//Formula V=4/3*pi (3.142857143) *dia cubed (1520.875) = volume 
			var Volume_Calc = (4 / 3 * Math.PI * (Math.pow((dia_in_mm / 2),3)));
		
			//Lead is 0.01134 gms per cu mm
			//Formula Vol*weight per gm = weight of sphere gms Show Result
			var Sphere_Weight_Calc = Volume_Calc * 0.01134;
			
			//convert gms Weight *28.346 (gms per ounce) = wt in ounces - SR
			var Weight_In_Ounces_Calc = Sphere_Weight_Calc * 28.346;
			
			//convert gms 72.27*15.43209877 (grains per ounce) = wt in grains SR
			var Grains_Per_Ounce_Calc = Weight_In_Ounces_Calc * 15.4320987;
			
			//convert 553.59237 (gms per ounce)/72.27 = Gauge Show result 
			var Gauge_Calc = Weight_In_Ounces_Calc / 72.27;
			
			myForm.Volume.value = Volume_Calc;
			myForm.Sphere_Weight.value = Sphere_Weight_Calc;
			myForm.Weight_In_Ounces.value = Weight_In_Ounces_Calc;
			myForm.Grains_Per_Ounce.value = Grains_Per_Ounce_Calc;
			myForm.Gauge.value = Gauge_Calc;
		}
    }
     
    </script>
     
    </head>
     
    <body>
    <h3 style="color:#F00">Enter the Diameter in Millimeters or Inches.<br />If you enter both then only the Millimeters will be used.</h3>
    <form>
    <input type="text" name="diaOfCircle_mm">Diameter of a circle in Millimeters<br />
    <input type="text" name="diaOfCircle_in">Diameter of circle in Inches<br /><br />
    
    <input type="text" name="Volume">Volume in cu mm<br />
	<input type="text" name="Sphere_Weight">Weight of sphere gms<br />
	<input type="text" name="Weight_In_Ounces">Weight in Ounces<br />
    <input type="text" name="Grains_Per_Ounce">Weight in Grains per Ounce<br />
    <input type="text" name="Gauge">Gauge<br /><br />
    <input name="Calculate" type="button" value="Calculate Guage" onClick="calculateTotal(this.form)">
    </form>
     
    </body>
     
    </html>

Hi
Dreamweaver has split it over 3 lines! instead of copying it as you have it here.
I'm guessing this is why I can see the form but can't perform any function.
Not being a coder,
Do I reset the code as you sent it?
Is the indent critical or just indicative of group functions?

The indent is just cosmetic. I have attached my file.

Ken

Tidied it all up and now working.
Must be a slight glitch in the maths so will check it later
Many thanks

Jon BH,

The attached object-oriented approach may be of interest. It's a bit different.

In addition to a basic calculator, I have coded a verifier so you can check the calculations against an independent source. You will see what I mean when you run the attached page.

There should be enough comments in the code for you to see at least roughly how it works.

Airshow

Hi
Don't know why but windows is telling me the zips are invalid and won't open them Grrrrrrrr tried every option, no joy :-(

I zipped with Windows7's built-in zipper.

Off to the supermarket right now.

Will address when I get back.

Airshow

Vista should be ok with that? or not?

Under Win7,

  • click > 'open' doesn't work
  • click > 'save' to desktop, then open from desktop works

If still problematic, I will paste-in-post instead.

Airshow

Under Win7,

  • click > 'open' doesn't work
  • click > 'save' to desktop, then open from desktop works

If still problematic, I will paste-in-post instead.

Airshow

Hi
No joy with either style of opening it ;-(

I downloaded it just to see it and it unzipped just fine for me. I am running Windows 7 and I used Zipeg to unzip it. Zipeg is free just google it.

Sounds like good advice k9h.

A

Sounds like good advice k9h.

A

Brilliant guys
Spot on with the calc and the unzip info. I think my machine was short of Java so did auto load and all went like a dream.
Just a couple of small points on the calc.

Can I set gauge to 1 decimal point only.
Also I'd like to have the conversion to grains showing if possible.

I presume I can dress it up a little for embedding in a page (not checked code yet as it's late - need a quiet day and a clear head)
Thank you so very much.
I'd like to put a credit tag on it, if that is, you'd like one.
Jon

If you plan on looking at my code then you will find a constructor called BoreObj. new BoreObj(x) , where x is a caliber in millimeters, returns an object with the following properties:

  • .caliber.mm : the originally given caliber in millimeters
  • .caliber.inch : the caliber in inches
  • .mass.g : the mass of a spherical lead shot in grams
  • .mass.lb : the mass of a spherical lead shot in pounds
  • .mass.oz : the mass of a spherical lead shot in ounces
  • .mass.gr : the mass of a spherical lead shot in grains
  • .gauge : the gauge (or bore), ie. the multiplicative inverse of mass.lb
  • .gaugeName : the name of the (approximate) gauge where it exists, eg, c, B, A, AA. For unnamed gauges, this property contains '' (empty string).

As it stands, the BoreObj() constructor does not include "poundage" (the mass of a spherical cast iron shot) but this could be easily added.

All properties are to full javascript precision. (I can't recall but it's some mega word length).

Exactly what you do with the returned object is up to you. Typically you will perform any further math at full precision, and/or round with eg. xxx.gauge.toFixed(1) (for 1 decimal place) for display.

To answer your specific questions:

  • Can I set gauge to 1 decimal point only? Yes
  • Also I'd like to have the conversion to grains showing if possible. Easy

Airshow

Here's a revised version of the file.

In the demo, the BoreObj() constructor is used in three ways:

  1. Calculator 1: To my (rather ugly) design.
  2. Calculator 2: Approximating to your design.
  3. To generate a table of gauge/caliber/shot mass at various sizes

You may find the gauge rounding algorithm in Calculator 2 and the tabulated data to be better than straightforward 1 decimal place, which becomes too coarse at calibers above about 45mm, and arguably too fine at calibers of less than about 0.78mm. The algorithm yields (about) two significant digits regardless of magnitude; at least within the range of interest.

Airshow

Hi
Final phase I think.
Calc 2 looks great and functions well. I note it corrects some printed tables which have variable inaccuracies.
I'd initially thought calc 1&2 were indepandant but guess they must be linked. In doing a simple edit I deleted calc 1 which tidied up my page but stopped calc 2 working.
Can I fix this or is it better that you trim it down so I don't break anything again?

I also cleared the "0" value in the millimeters box so users didn't have to delete it before entry but haven't managed to do the same with the inches box which might be tidier just in case they wanted to input to that box instead and didn't have to clear it first.

It's OK, the two forms are independent but if you remove the HTML for the first, javascript throws an error when it tries to initialise what is no longer there.

So you must also delete the 12 concecutive javascript lines starting with //Calculator 1 .

To make both the mm field and inches field initially blank:

  • remove value="0" from the mm field's HTML (I expect you have done this already).
  • comment out (or remove) the line elements.bore_mm.onkeyup(); .

(You should do similar after removing the verification table, though the js error will be thrown after Calc2's handlers are put in place).

After testing this, in Opera I found the three weight fields (tds) were very shallow in the absence of values, so needed to give them height with CSS;

form#f2 td.val {
	height: 1.5em;
	.....
	.....
}

Alternatively, you could seed these three fields with zeros in HTML.

Airshow

Got it and much tidier. Not too concerned with zeroing the results boxes though. Think keep it simple.
Just one little thing.
Initial input boxes both at zero.
Enter data, do calc fine.
Then clear either or input box for next calc and the uncleared box reverts to zero. Any way round this? Not a massive issue.
Otherwise great ;-))
I'm sure users will find it of great benefit. Also it's resolved the issue of some other inaccurate tables which can be found on line.
Really appreciate the help with this one.
Many thanks

... Then clear either or input box for next calc and the uncleared box reverts to zero. Any way round this? Not a massive issue.

This is the natural behaviour put in place by the two .keyup functions, which treat blank inputs as 0 and convert accordingly.

To ensure that blanks are mutually reflected as blank in the opposite field, then they each need to be treated as a special case with regard to diameter conversion whilst still passing zero to calc (otherwise it would give an error). Try this patch:

elements.bore_mm.onkeyup = function(){
		var val = Number(this.value);
		elements.bore_inch.value = (this.value==='') ? '' : (val / k.mmPerInch).toFixed(3);
		calc(val);
		return false;
	};
	elements.bore_inch.onkeyup = function(){
		var val = Number(this.value) * k.mmPerInch;
		elements.bore_mm.value = (this.value==='') ? '' : val.toFixed(3);
		calc(val);
		return false;
	};

Airshow

Very sweet and tidy.
I also took the verification chart and set it as a printable page.
My only tiny concern is the weight / volume of lead. When I looked this up I found 2 different values over several reference points but went with the majority.
I'm sure someone will pick this up if I was wrong.
I think we can sign this off now.
So many thanks
Really appreciate your help

The generally agreed density of lead at room temperature seems to be 11.34 g.cm^-3 (0.01134 g.mm^-3).

I can only assume that other values are either incorrect or apply to lead at a different temperature.

You should be able for formally sign this off by marking "solved". (I don't know exactly how because I have never done it).

Good luck with the rest of your project.

Airshow

Once again
Really appreciate the help
Will sign it off now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.