Jeff Tran
Jeff Tran

Reputation: 33

math is off in Batch

I have been trying to set up a simple weight calculator for co-workers. I know there are plenty on the internet, but I wanted them to enjoy something that came from in house.

I currently am experiencing two problems using my statistics.

My weight is 180 my height is 60 and my age is 42.

I use the current formula age+(6.23 x height)+(12.7 x weight)-(6.76 x age)

the first section should show 1121 but instead calculates 1080 the second section should show 762 but instead calculates 720 the third section should show 284 but instead 252

I also get operand issues or missing operator issues and would ask for a little hope. Eventually I will include the women's calculation in also and then give it a nice little frontend. Help would be greatly appreciated.

here is my code so far.

@echo off
setlocal enabledelayedexpansion
:age
Set /p age= What is your age?:  
SET /P ANSWER= you entered %age% is this correct? (Y/N)?  
if /i {%ANSWER%}=={y} (goto :weight)  
if /i {%ANSWER%}=={yes} (goto :weight)  
if /i {%ANSWER%}=={n} (goto :age)  
if /i {%ANSWER%}=={no} (goto :age)  
:weight  
Set /p weight= What is your weight?:  
SET /P ANSWER= you entered %weight% is this correct? (Y/N)?  
if /i {%ANSWER%}=={y} (goto :height)  
if /i {%ANSWER%}=={yes} (goto :height)  
if /i {%ANSWER%}=={n} (goto :weight)  
if /i {%ANSWER%}=={no} (goto :weight)  
:height  
Set /p height= What is your Height in Inches?:  
SET /P ANSWER= you entered %height% is this correct? (Y/N)?  
if /i {%ANSWER%}=={y} (goto :calcs)  
if /i {%ANSWER%}=={yes} (goto :calcs)  
if /i {%ANSWER%}=={n} (goto :height)  
if /i {%ANSWER%}=={no} (goto :height)  
:calcs  
set /A calc1= %weight%*6.23  
echo weight = %calc1%  
set /A calc2= %height%*12.7  
echo height = %calc2%  
set /A calc3= %age%*6.76  
set /A calc4= %age%+%calc1%  
echo age = %calc3%  
set /A calc5 = %calc4%+%calc2%  
set /A Result= %calc5%-%calc3%  

echo your caloric intake should be %Result%  

Upvotes: 2

Views: 149

Answers (2)

Jeff Tran
Jeff Tran

Reputation: 33

so the full answer to the question was this.

my original equation used decimals which Aacini pointed out does not work. So he added the +50 to compensate for the decimal and made them whole numbers. then divided by 100 at the end removing the two decimal points.

This is the rendition that was sent over that did a better job.

set /A Result=( age + height*623 + weight*1270 - age*676 + 50 ) / 100

age was replaced with + 66 and moved to the end for proper calcs. May not have needed the extra parenthesis but wanted to be sure.

set /A MResult=( (%weight%*623) + (%height%*1270) - (%age%*676) + 50 ) / 100 + 66

age (for the women's) was replaced with 655 and moved to the end, this equation worked great.

set /A FResult=((%weight%*435) + (%height%*470) - (%age%*470) + 50 ) / 100 + 655  

Thanks for the help Aacini

Upvotes: 0

Aacini
Aacini

Reputation: 67216

Although the SET /A command can only manage integer numbers, there are several ways to perform floating point arithmetic operations in Batch files, even trigonometric and logarithms! However, you just need a very simple operation with two decimal places that may be achieved this way:

:calcs
set /A Result=( age + height*623 + weight*1270 - age*676 + 50 ) / 100

echo your caloric intake should be %Result% 

Note that the + 50 is equivalent to add 0.5 for rounding the final sum.

Upvotes: 4

Related Questions