1

I need to print values that are in a matrix, these may vary from integer to real. As an example a matrix that my program use, named kernel, is shown below:

kernel[0,0]:= 1/16;
kernel[0,1]:= 2/16;
kernel[0,2]:= 1/16;
kernel[1,0]:= 2/16;
kernel[1,1]:= 4/16;
kernel[1,2]:= 2/16;
kernel[2,0]:= 1/16;
kernel[2,1]:= 2/16;
kernel[2,2]:= 1/16; 

the issue comes when printing each of them, because I couldn't find a way to print a number like 1/16 in a easy-to-read way, the program displays something like 6.2500000000000000000000000000E-2 which is OK but I would prefer to have something more aesthetic like 0.0625 or even better 1/16. Does anyone acknowledge a way of formatting that allows me to do so?

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • `1/16` is not a number, it's an operation. If you want to display `1/16`, you'll need to do so in a string. You can use `RoundTo` to truncate to a specific number of decimal places, but doing so will affect the accuracy of calculations with the value. For display purposes, you can use `FormatFloat` or `Format` to only display part of the decimal values wihtout any loss of accuracy in the value itself. – Ken White Mar 23 '21 at 23:45
  • Accuracy is not something that I care much about in this context, so I'm gonna use RoundTo... Thank you Ken – neil_huygens Mar 23 '21 at 23:47
  • You might when you try to add them up again. :-) Also, `2/16` is properly `1/8`, and `4/16` is `1/4`. – Ken White Mar 24 '21 at 01:07
  • If you problem is to display the numbers, you can use Format('%5.3f', [Kernel[0,0]); which returns a string with the number "formatted" with a length of 5 and 3 decimal places. 1/16 will becomes "0.625". There are a lot of options, look at the documentation http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.Format – fpiette Mar 24 '21 at 06:20
  • 2
    Maybe this is want you are looking for: https://stackoverflow.com/questions/5124743/algorithm-for-simplifying-decimal-to-fractions – fpiette Mar 24 '21 at 06:24
  • `RoundTo` doesn't seem like the right solution to me. This is something that you should do at the formatting stage. You don't need to make a new floating point value. – David Heffernan Mar 24 '21 at 09:22
  • What do you know about the denominator? If it's always 16 then it should be easy to display as a fraction. Just multiply the number by 16 and then write that number divided by 16. If the denominator can be anything then it's more difficult. Writing 1/3 as a fraction is difficult because precision will be lost when stored as a single number. The best way when dealing with inputs and outputs as fractions is to store the numerators and denominators separately. – XylemFlow Mar 24 '21 at 09:42

1 Answers1

2

Each array element holds the value of the fraction.

To write the value as a fraction you can simply calculate the numerator as the product of the array element and the denominator.

I prefer to use the Format() function for display formatting.

Given the array you show, two index variables, a and b and the denominator

denominator := 16;
for a := 0 to 2 do
  for b := 0 to 2 do
  begin
    s := format('kernel[%d,%d] = %d/%d',[a,b,round(kernel[a,b]*denominator),denominator]);
    memo1.Lines.Add(s);
  end;
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54