* * *

Author Topic: BMI index RadioGroup  (Read 501 times)

godik

  • New member
  • *
  • Posts: 14
BMI index RadioGroup
« on: October 26, 2018, 09:37:27 pm »
Hello I have problem my bmi index is kinda broken i dont know why :) And i dont really understant this command
RadioGroup1.Caption:='BMI = '+floattostr(bmi);





procedure TForm1.Button1Click(Sender: TObject);
var heightm,weightkg,bmi:real;
begin
  heightm:=StrToFloat(edit1.text);
  weightkg:=StrToFloat(edit2.text);
  bmi:=weightkg/heightm;
  RadioGroup1.Caption:='BMI = '+floattostr(bmi);
  if bmi<=19.9 then RadioGroup1.ItemIndex:=0;
  if bmi<=24.9 then RadioGroup1.ItemIndex:=1;
  if bmi<=29.9 then RadioGroup1.ItemIndex:=2;
  if bmi<=39.9 then RadioGroup1.ItemIndex:=3;
   if bmi >40 then RadioGroup1.ItemIndex:=4;   

Thanks for all answers

jamie

  • Hero Member
  • *****
  • Posts: 908
Re: BMI index RadioGroup
« Reply #1 on: October 26, 2018, 09:52:44 pm »
numbers are not stored in memory like they are on paper when  you look at them.

 A REAL is a floating point number that is stored in memory in a non human readable format. The FloatToStr creates a
human readable list of letters (characters) so that you can better understand the value when looking at it.

The "Caption" is a string which shows at the top of the group box and what is happening is you are directly telling it
to build a string (a series of letters) of what is inside the '?????' and adding the converted results of the FloatToStr to
create a final single string which will be stored in the "Caption"

 the FloatTostr function could be creating a number with a power at the end "-10" for example. This can be fixed by using a
different formatting function or use the "Str" function which will generate a string with a specified format..

  Str(Hmi, 10:2, OutPutString);
 
 That will create a number string 10 characters long with 2 places to the right after the decimal point.

 If you need more info feel free to come back.

Blaazen

  • Hero Member
  • *****
  • Posts: 2654
  • POKE 54296,15
    • Eye-Candy Controls
Re: BMI index RadioGroup
« Reply #2 on: October 26, 2018, 10:08:35 pm »
The height in formula must be squared:
Code: Pascal  [Select]
  1.   bmi:=weightkg/(heightm*heightm);
or
Code: Pascal  [Select]
  1.   bmi:=weightkg/power(heightm, 2);

See: https://en.wikipedia.org/wiki/Body_mass_index
Lazarus 2.1.0 r59474M FPC 3.3.1 r40247 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.14

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

godik

  • New member
  • *
  • Posts: 14
Re: BMI index RadioGroup
« Reply #3 on: October 27, 2018, 10:22:05 am »
Thank you guys appreciate it  :)

BrunoK

  • Jr. Member
  • **
  • Posts: 70
Re: BMI index RadioGroup
« Reply #4 on: October 27, 2018, 10:36:53 am »
Except for the BMI index where Blaazen's indication seems right, the main problem that godik has in his code logic is that the index can be only 3 or 4.

Conditions should be
Code: Pascal  [Select]
  1. if bmi<=19.9 then RadioGroup1.ItemIndex:=0
  2. else  if bmi<=24.9 then RadioGroup1.ItemIndex:=1 //  <- else for other cases
  3. else etc...
  4. else
  5.   RadioGroup1.ItemIndex:=4;  
  6.  
Lazarus trunk 2018.10.01 (+/- patches regarding enabled). FPC 3.0.4 32 bits. Windows 10 Pro x64 (v. 1803)

jamie

  • Hero Member
  • *****
  • Posts: 908
Re: BMI index RadioGroup
« Reply #5 on: October 27, 2018, 03:42:16 pm »
Looks like he messes the number 40 on the last check...

it should be >= 40;

currently it's > 40;
and the one before is  <= 39.9

 that is what I observed anyways.


Kays

  • Full Member
  • ***
  • Posts: 113
  • Whasup!?
    • KaiBurghardt.de
Re: BMI index RadioGroup
« Reply #6 on: October 27, 2018, 04:43:01 pm »
[…] bmi index […]
[…] BMI index […]
Argh! “BMI Index” Dudes, the “I” already stands for “index.”

I just wanna mention, don't forget to implement a plausibility check. The last time I've seen the GP, we measured size and weight, and their program accepted values in the wrong fields, suggesting a BMI of several thousands.
Yours Sincerely
Kai Burghardt

jamie

  • Hero Member
  • *****
  • Posts: 908
Re: BMI index RadioGroup
« Reply #7 on: October 27, 2018, 08:43:17 pm »
We can always find more stuff wrong! :D

 With that type of code frag, its highly possible there is things like 39.888889 etc taking place
or 20.0000000556 etc... if you know what I mean...
 working with floats are fun since they can't represent the exact number and give you something just a little different..

  Which is why its best to use a Currency or integer multiplied  X 10 for your one fractional digit to the right, that way you
get exactly what you want.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus