Writeln output extended data type as floating-point not as Scientific Expression

formatting output in pascal
pascal writeln
pascal format string
pascal data types
pascal code list
pascal print
free pascal syntax
type var pascal

Good night, I'm having a issue with writeln on Embarcadero 10.2, I'm trying to write the sin() function as a Taylor expansion infinite serie.

Everything it's running fine, but the output is being in Scientific Expression like: 3.60448486921676E-0158 when the correct was 0.912945250727627654376099983845.

I need a 30 digits precision. My code below, It's a console program.

program Project1;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  Windows,
  Math;

function fact(n: LongInt): extended;
begin
  if (n = 0) then
     fact := 1
  else
     fact := n * fact(n - 1);
end;

var
  one:integer;
  fin:extended;
  number:LongInt;
  i:integer;
  cons:LongInt;
  cons1:extended;
begin
  one := -1;
  writeln('Digite o angulo em radianos para o Seno: ');
  readln(number);
  for i := 1 to 120 do
  begin
    cons := (2*i)+1;
    if(i mod 2) = 0 then 
      one := 1
    else 
      one := -1;
    cons1 := fact(cons);
    fin := (one/cons1)*(power(number,cons));
    cons := 0;
  end;
  writeln(fin);
  readln;

end.
program Project1;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;


function sine(n: real): real;
asm
fld n
fsin
fstp Result
end;

var
n:real;
begin
write('Write the number: ');
readln(n);
writeln(sine(n):10:11);
readln;
end.

Encyclopedia of Computer Science and Technology, However, Algol lacked a full range of data types and other features needed for title (characters), ID number (a long integer), and salary (a floating-point number). The rigorous use of data types also extends to the way procedures are called and That total is then used to compute the average, which is output by the writeln  All floating-point numeric types are value types. They are also simple types and can be initialized with literals. All floating-point numeric types support arithmetic, comparison, and equality operators. Characteristics of the floating-point types. C# supports the following predefined floating-point types:

Computing sin(x) with Taylor expansion is stable only for small arguments, say less than Pi/2. For larger arguments you suffer from catastrophic cancelation, because intermediate terms are very large, e.g. for x=100 the largest terms is the 49'th with a value 0.107151028812546692318354675952E43. All these large terms are added together and give a sin() value with magnitude <= 1. You would need about 70 decimal digits, do compute the result with about 30 digits.

The solutions is: Use range reduction and multiprecision floating-point routines (see mpf_sin in my opensource Pascal MPArith package). Using the interactive calculator you get

sin(100) = -0.5063656411097587936565576104597854321 in 0.2 ms.

Formatting output, If the width is not long enough for the data, the width specification will be you can use the aforementioned syntax to display scientific notation in a specified field width, or you can convert to fixed decimal-point notation with:. Why not use a fixed point representation instead of a floating point? floating point? A. Fixed point numbers have a fixed number of digits after the decimal place. Can be represented using integer arithmetic.

To perform calculation with very long precision, you need some library for arbitrary precision arithmetics - use Delphi wrapper/interface to GMP or some Delphi library. Example. Another one.

Note that for libraries with only long integers support (not floats) you still can calculate series like this Python code (it has internal support for big integers. ** is power, // is div). The only division (slow operation) is performed once.

d30 = 10**30
d60 = d30 * d30
x = 3141592653589793238462643383279 // 4   #Pi/4 * d30
xx = - x * x
denom = d30
nom = x
sum = nom
i = 2
while abs(nom) * d30 > denom:
    nom =  nom * xx
    mul = i * (i + 1) * d60
    denom = denom * mul
    sum = sum * mul + nom
    i += 2

print('0.' + str(d30 * sum // denom))

>> 0.707106780551955811469384356540

Approach is based on this: x - x^3/6 + x^5/120 = (x*120 - x^3 * 20 + x^5) / 120

Write, Write variable to a text file the variables V1 , V2 etc. must be of the same type as the type in the declaration of F . Untyped files are not allowed. Any floating-​point type (such as double, single, extended). If the parameter F is omitted, standard output is assumed. Real numbers are, by default, written in scientific notation. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

About Floating-Point Arithmetic, For integral values (integer types), you must consider the possibility of For floating-point values (single, double or extended precision), you must floating-​point variable, the floating-point variable might not have sufficient bits to SysUtils; var X: Single; Y: Double; begin X := 0.1; Y := 0.1; Writeln('X =', X); Console output:. There are three different floating point data types: float, double, and long double. As with integers, C++ does not define the actual size of these types (but it does guarantee minimum sizes). As with integers, C++ does not define the actual size of these types (but it does guarantee minimum sizes).

Analyzing the loss of significance in floating-point computations , Basically, we are writing the number in an (almost-)scientific notation like Since a double-precision floating point has 11 bits for the signed integer exponent, we WriteLine(" decimal sign exponent mantissa"); Console. a direct access to those extended data formats, for example the long double type in  All identifiers in the list are declared as being of the same data type. The basic data field data types in Pascal include: Integer; Word; LongInt; Real; Char; Boolean; Standard Pascal does not make provision for the string data type, but most modern compilers do.

Comparison of floats (if a>b then), float precision, Delphi Help says that number is -3.6 x 10^4951, but this is not enough if one wants to be precise Delphi works in scientific notation with numbers, as I'm sure you've figured out A great demonstration you can try is to define an extended value, Currency is a great data type to have for attempting correct  The Int64 and QWord type. Any floating-point type (such as double, single, extended). If the parameter F is omitted, standard output is assumed. If F is of type Text, then the necessary conversions are done such that the output of the variables is in human-readable format. This conversion is done for all numerical types.

Comments
  • Delphi does not have a floating point type with 30 digits precision. See the documentation
  • This is a interesting topic. I also never could show extended as floating... How can I do it with like 10~15 digits? In C it's normal
  • For non-scientific notation, try something like Writeln(MyExtended:30:18);
  • Pascal RTL contains Sin function, so why use asm? And what about 30 digits precision? ;-)
  • Are you aware that your code uses double and not extended variables? So you can roughly get only half of the required precision. Are you aware the x87 FPU cannot handle arguments >= 2^63? Have you ever tried to compute sine(ldexp(1,63)) and wonder why the result is a very large number (9223372036854775810.0)?
  • I edited the link to my BigIntegers to point to my BigDecimals. Probably better suited for such a task. I have already done cosines up to 10,000 digits with no problem (and checked them with online resources). Just take a few extra digits of precision (I found 5 to be more than sufficient, even for 10,000 digits) and round back to the desired precision at the end.