Count decimal place lazarus / 61-Beginners ---------------------------------------------------------------------- bigeno · 2019-05-24 10:21 What is the best way to count decimal place/precision for number ? as example Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  x := decplaces(2.23000); //should return 2 x := decplaces(2.2002); //should return 4etc. for now I think to  multiply by 10 until frac will = 0, but maybe there is better way ? ---------------------------------------------------------------------- wp · 2019-05-24 10:41 Some effort for "nice" string presentation has been put into the function FloatToStr. Therefore, I would convert the number to string and count the characters following the decimal separator: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   function CountDecimals(value: Double): Integer;  var    s: String;    p: Integer;  begin    s := FloatToStr(Value);    p := pos(FormatSettings.DecimalSeparator, s);    if p = 0 then      Result := 0    else      Result := Length(s) - p;  end; However, usually floating point numbers cannot be expressed exactly like this by the computer due to the final resolution of floating point data types. While most of these issues seem to be covered by the FloatToStr function there is still a risk that many more decimals will be seen than what is expected. ---------------------------------------------------------------------- bigeno · 2019-05-24 11:00 count the characters ... after all it works in my case until 13 decimal places works ok, but in my case user can enter max 11 so good in my case thx ---------------------------------------------------------------------- Zvoni · 2019-05-24 13:01 Hrhrhr 1. Convert To String 2. Split along Decimal separator --> The High-End of the resulting String-Array contains your fraction 3. String-Reverse the Fraction 4. Convert to Integer (so any leading Zeros in the reversed Fraction get eliminated --> those were trailing zeros in the original order) 5. Convert back To string 6. Check String-Length 7. Done ---------------------------------------------------------------------- engkin · 2019-05-24 15:51 Quote from: Zvoni on May 24, 2019, 03:01:40 pmHrhrhr 1. Convert To String 2. Split along Decimal separator --> The High-End of the resulting String-Array contains your fraction 3. String-Reverse the Fraction 4. Convert to Integer (so any leading Zeros in the reversed Fraction get eliminated --> those were trailing zeros in the original order) 5. Convert back To string 6. Check String-Length 7. Done Did you see wp's code? It was almost like: 1,2 and 6 (he did not need to split, just used pos) The challenge is to find the answer without converting to string. ---------------------------------------------------------------------- Bart · 2019-05-24 16:39 Quote from: engkin on May 24, 2019, 05:51:56 pmThe challenge is to find the answer without converting to string.  How? For the compiler 2.0 and 2.0000 the same. '2.0' and '2.0000' are not. Bart ---------------------------------------------------------------------- engkin · 2019-05-24 17:26 According to the original post: decplaces(2.23000); //should return 2 ---------------------------------------------------------------------- bigeno · 2019-05-24 17:56 String converting is ok for me. But my first try was numerical Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function decplaces2(const val: extended): integer;var  i: integer;  s: extended;begin i := 0; s := val; while i < 20 do begin    if IsZero(frac(s)) then exit(i);    s := s * 10;    Inc(i); end;end; and that code gave me insane results. Round off errors ? ---------------------------------------------------------------------- winni · 2019-05-24 19:55 The documentation is not quiet clear, but http://wiki.freepascal.org/Function says, you must set Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objFPC} to set the exit parameter as function result . Winni ---------------------------------------------------------------------- ASerge · 2019-05-24 20:26 Quote from: bigeno on May 24, 2019, 07:56:09 pmRound off errors ? Representations of floating numbers are not accurate. This simple code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE}var  R: ValReal = 1.2;begin  Writeln(R:0:19, ' frac:', Frac(R):0:19);  Readln;end.On Windows x64 gives the result: Code: [Select]1.2000000000000000000 frac:0.1999999999999999600 ---------------------------------------------------------------------- wp · 2019-05-24 20:31 Quote from: bigeno on May 24, 2019, 07:56:09 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function decplaces2(const val: extended): integer;var  i: integer;  s: extended;begin i := 0; s := val; while i < 20 do begin    if IsZero(frac(s)) then exit(i);    s := s * 10;    Inc(i); end;end; and that code gave me insane results. Round off errors ? I don't see a problem in this code. When you look into the sources of IsZero (just CTRL-click on this identifier to open unit Math at its implementation) you'll see that calling this function without a second parameter means application of a tolerance for the zero-check of 1E-16. This means that round-off error below 1E-16 should be handled correctly. If this is too sensitive just use your own epsilon in IsZero: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  EPS = 1E-9;...  if IsZero(frac(s), EPS) then exit(i);... ---------------------------------------------------------------------- ASerge · 2019-05-24 20:43 Quote from: wp on May 24, 2019, 10:31:57 pmI don't see a problem in this code. See my example above. The first call to the Frac function increased the number of digits from 1 to 17. A subsequent call to the Frac function can increase this number to 50. IsZero will help only in really late, i.e. not 50, but 48, for example. Either way, it's a problematic code. ---------------------------------------------------------------------- wp · 2019-05-24 20:49 After the last "real" decimal the frac will be a very close to zero and will be caught by the epsilon of the IsZero function, even for the default epsilon of 1E-16 for extended values. This code is working correctly for me: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  math; function decplaces2(const val: extended; Epsilon: Extended = 0.0): integer;var  i: integer;  s: extended;begin i := 0; s := val; while i < 20 do begin    if IsZero(frac(s), Epsilon) then exit(i);    s := s * 10;    Inc(i); end;end; const  EPS = 1E-9;var  a: extended;begin  a := 1.2;  WriteLn('Full presentation of a: ', a);  WriteLn('Decimal places: ', DecPlaces2(a));  WriteLn('Decimal places with EPS: ', DecPlaces2(a, EPS));   ReadLn;end. ---------------------------------------------------------------------- ASerge · 2019-05-24 21:07 Quote from: wp on May 24, 2019, 10:49:52 pmThis code is working correctly for me: My apologies, you're right. The algorithm multiplies the original number, not the fractional part, so that rounding does not accumulate and the result is correct.