POV-Ray : Newsgroups : povray.general : Number crunching in POV SDL Server Time
31 Oct 2024 14:10:18 EDT (-0400)
  Number crunching in POV SDL (Message 1 to 1 of 1)  
From: PM 2Ring
Subject: Number crunching in POV SDL
Date: 27 Jul 2005 05:20:00
Message: <web.42e75049510f4b2eef8bd73c0@news.povray.org>
Here's another task that is not normally performed by ray tracers: high
precision number crunching. The following scene calculates the number e,
the base of natural logarithms, to a large number of digits. This algorithm
can be used to calculate millions of digits, if desired. If you do extend
it, just watch out for overflow: you may need to reduce the block size.

// Persistence of Vision Ray Tracer Scene Description File
// File: ECalc.pov
// Vers: 3.6
// Desc: High precision calculation of e, the base of natural logarithms,
//       using 'factorial base' method.
// Date: 2004.09.05
// Auth: PM 2Ring
//
//
// -F -A0.5 +AM2 +R1
// -D +A0.1 +AM2 +R3
//

#declare Num = 1000;            //Total number of decimal digits
#declare BS = 5;                //Number of digits per block

#declare Use_Light = 1;         //0=self-luminous. 1=use light source
#declare Use_Sky = 1;           //0=grey background. 1=Sky sphere with
clouds

//-------------------------------------------------------------

#include "colors.inc"
#include "skies.inc"

camera{
  //orthographic
  location -z * 70
  look_at 0
  right x*image_width/image_height up y
  direction z
  angle 30
}

#if(Use_Sky)
  sky_sphere {S_Cloud2 rotate <65, -175, -3> scale .25}
#else
  //background{rgb .5}
#end

//0: self luminous
#if(Use_Light)
  light_source {<0.0, 150.0, -250.0> rgb 1}

  #default {
    finish{
      specular 0.75 roughness 0.045
      phong .5 phong_size 200
      metallic
      reflection{
        0.6
        metallic
      }
      ambient 0.05 diffuse 0.7
    }
    pigment{ rgb<1.0, 0.75, 0.175> }
  }
#else
  #default {
    finish{ ambient 1 diffuse 0 }
    pigment{ rgb<1.0, 0.8, 0.2> }
  }
#end

//-------------------------------------------------------------

//Progressively print passed strings, with line wrap. Can't break up long
strings.
#declare Txm=18;                //Right margin
#declare Lx=-Txm;               //Left margin
#declare Tx=Lx;                 //Cursor position
#declare Ty=11.5;               //Top margin
#declare Dy=1.1;                //Line height

//Newline
#macro NL()
  #declare Tx=Lx;
  #declare Ty=Ty-Dy;
#end

//Print string
#macro print(s)
  #local TBlock = text{ttf "lucon", s .5, 0}

  #local Dx = (max_extent(TBlock) - min_extent(TBlock)).x;
  #if(Tx+Dx>= Txm) NL() #end

  object{TBlock translate <Tx, Ty, 0>}

  #declare Tx=Tx + Dx;
#end

//-------------------------------------------------------------

//Inverse log10 factorial using Newton's method.
#macro InvLFact(n)
  #local a = n * ln(10) - .5 * ln(2 * pi);
  #local k = (n<1 ? 2 : a);
  #local i=0;
  #while (i<3)
    #local k = (a + k + 1/(24 * k)) / ln(k);
    #local i=i+1;
  #end
  (k - .5)  //return value
#end

//-------------------------------------------------------------

#declare SC = pow(10,BS);               //Block multiplier
#declare K = 2 + int(InvLFact(Num));    //Factorial required for this many
digits

//Initialize array with e in factorial base, i.e, the Taylor series
numerators.
#declare A = array[K+1];
#declare I=K;
#while (I>1)
  #declare A[I] = 1;
  #declare I=I-1;
#end

union{
  print(concat("    e to ", str(Num,0,0), " decimal places.")) NL()
  print("2.") NL()

  //#debug "Calculating...\n"
  #while (Num>1)
    #declare I=int(2 + InvLFact(Num));    //Factorial required for this many
digits
    #declare C=0;                         //carry
    #while (I>1)
      #declare T=C + A[I] * SC;
      #declare C = int(T/I);
      #declare A[I] = T - C*I;

      #declare I=I-1;
    #end
    print(concat(" ", str(C,-BS,0)))

    #declare Num = Num - BS;              //Number of digits left
    //#debug ".\n"
  #end
  //#debug "\nFinished\n"

  rotate -20*y
  translate 1.5*x
}

//--------------------End of scene-----------------------------


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.