File: base.gpt

package info (click to toggle)
gpt 1.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,128 kB
  • sloc: sh: 9,899; cpp: 6,783; makefile: 146
file content (84 lines) | stat: -rw-r--r-- 1,514 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Biblioteca padrão da linguagem G-Portugol (v1.0).
 * Autor: Thiago Silva
 *
 * Nota: as funções dessa biblioteca suprem apenas
 * funcionalidades e cenários simples. Melhorias e 
 * adições são bem vindas.
 * 
 */

função arredonda(num: real) : real
  trunc : inteiro;
início
  trunc := num + 0.5;
fim

função absoluto(num: real) : real
início
  se num < 0 então
    retorne -num;
  senão
    retorne num;
  fim-se
fim

função potencia(base: real, exp: real) : real
  i    : inteiro;
  res  : real;
  expi : inteiro;
início
  se exp = 0 então
    retorne 1;
  fim-se

  se exp = 1 então
    retorne base;
  fim-se

  se exp = -1 então
    retorne 1 / base;
  fim-se

  res := 1;
  expi := exp;
  para i de 0 até expi-1 faça
    res := res * base;
  fim-para
  retorne res;
fim

/*
 * Encontra a raiz usando o método Newton-Raphson
 * baseado no código da página:
 * http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C)
 */
função raiz_quadrada(num: real) : real
  x     : real;
  xn    : real;
  iters : inteiro;
  i     : inteiro;
  n     : inteiro;
  val   : real;
início
  n := num;
  para i de 0 até n faça
    val := i * i - num;
    se val = 0 então
      retorne i;
    fim-se  

    se val > 0 então
      xn := (i+(i-1))/2.0;
      i := n+1; //sair do loop!
    fim-se
  fim-para

  enquanto não ((iters  >= 100) ou (x = xn)) faça
    iters := iters + 1;
    x := xn;
    xn := x - (x * x - n) / (2 * x);
  fim-enquanto
  
  retorne xn;
fim