ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
fp_cos.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36#include <math.h>
37
38/****************************************************************************
39 * CODE SECTION
40 ****************************************************************************/
41
42/* <usermanual>
43@keyword COS
44
45@english
46This function will calculate the cosine value of an angle. The cosine of an acute
47angle are defined in the context of a right triangle: for the specified angle,
48its sine is the ratio of the length of the side that is opposite that angle to
49the length of the longest side of the triangle (the hypotenuse), and the cosine
50is the ratio of the length of the adjacent leg to that of the hypotenuse.
51
52More generally, the definitions of sine can be extended to any real
53value in terms of the lengths of certain line segments in a unit circle.
54
55The cosine functions are commonly used to model periodic phenomena such
56as sound and light waves, the position and velocity of harmonic oscillators,
57sunlight intensity and day length, and average temperature variations throughout
58the year.
59
60@italian
61
62Questa funzione calcolerà il valore del coseno di un angolo. Il coseno di un angolo
63acuto è definito nel contesto di un triangolo rettangolo: per l'angolo specificato,
64il suo seno è il rapporto tra la lunghezza del lato opposto a quell'angolo e la lunghezza
65del lato più lungo del triangolo (l'ipotenusa), e il coseno è il rapporto tra la lunghezza
66del cateto adiacente e quella dell'ipotenusa. Più in generale, le definizioni di coseno possono
67essere estese a qualsiasi valore reale in termini di lunghezze di alcuni segmenti di linea in
68una circonferenza unitaria.
69
70Le funzioni sinusoidali sono comunemente utilizzate per modellare fenomeni periodici come
71onde sonore e luminose, la posizione e la velocità degli oscillatori armonici, l'intensità
72della luce solare e la durata del giorno e le variazioni di temperatura media durante tutto l'anno.
73
74@syntax = COS(angle)
75
76@example x = COS(PI/2)
77
78@usedInExample float_cos.bas
79
80@target all
81</usermanual> */
82Variable * fp_cos( Environment * _environment, char * _angle ) {
83
84 Variable * angle = variable_retrieve_or_define( _environment, _angle, VT_FLOAT, 0 );
85 Variable * result = variable_temporary( _environment, VT_FLOAT, "(cos)");
86
87 switch( result->precision ) {
88 case FT_FAST:
89 if ( _environment->floatType.angle == FT_DEGREE ) {
90 Variable * pi = variable_temporary( _environment, VT_FLOAT, "(float)" );
91#if defined(__c128z__) || defined(__vg5000__) || defined(__zx__) || \
92 defined(__coleco__) || defined(__cpc__) || defined(__sc3000__) || \
93 defined(__sc3000__) || defined(__sg1000__) || defined(__msx1__) || \
94 defined(__gb__)
95 variable_store_float( _environment, pi->name, M_PI );
96#else
97 cpu_move_32bit( _environment, "PI", pi->realName );
98#endif
99 Variable * d180 = variable_temporary( _environment, VT_FLOAT, "(d180)" );
100 variable_store_float( _environment, d180->name, 180.0 );
101 Variable * radianAngle = variable_temporary( _environment, VT_FLOAT, "(cos)");
102 cpu_float_fast_mul( _environment, angle->realName, pi->realName, radianAngle->realName );
103 cpu_float_fast_div( _environment, radianAngle->realName, d180->realName, radianAngle->realName );
104 cpu_float_fast_cos( _environment, radianAngle->realName, result->realName );
105 } else {
106 cpu_float_fast_cos( _environment, angle->realName, result->realName );
107 }
108 break;
109 case FT_SINGLE:
110 if ( _environment->floatType.angle == FT_DEGREE ) {
111 Variable * pi = variable_temporary( _environment, VT_FLOAT, "(float)" );
112#if defined(__c128z__) || defined(__vg5000__) || defined(__zx__) || \
113 defined(__coleco__) || defined(__cpc__) || defined(__sc3000__) || \
114 defined(__sc3000__) || defined(__sg1000__) || defined(__msx1__) || \
115 defined(__gb__)
116 variable_store_float( _environment, pi->name, M_PI );
117#else
118 cpu_move_32bit( _environment, "PI", pi->realName );
119#endif
120 Variable * d180 = variable_temporary( _environment, VT_FLOAT, "(d180)" );
121 variable_store_float( _environment, d180->name, 180.0 );
122 Variable * radianAngle = variable_temporary( _environment, VT_FLOAT, "(cos)");
123 cpu_float_single_mul( _environment, angle->realName, pi->realName, radianAngle->realName );
124 cpu_float_single_div( _environment, radianAngle->realName, d180->realName, radianAngle->realName );
125 cpu_float_single_cos( _environment, radianAngle->realName, result->realName );
126 } else {
127 cpu_float_single_cos( _environment, angle->realName, result->realName );
128 }
129 break;
130 }
131
132 return result;
133
134}
void cpu_float_single_cos(Environment *_environment, char *_angle, char *_result)
Definition 6309.c:7272
void cpu_float_fast_div(Environment *_environment, char *_x, char *_y, char *_result)
Definition 6309.c:7228
void cpu_float_fast_cos(Environment *_environment, char *_angle, char *_result)
Definition 6309.c:7268
void cpu_move_32bit(Environment *_environment, char *_source, char *_destination)
CPU 6309: emit code to move 32 bit
Definition 6309.c:2520
void cpu_float_fast_mul(Environment *_environment, char *_x, char *_y, char *_result)
Definition 6309.c:7207
void cpu_float_single_div(Environment *_environment, char *_x, char *_y, char *_result)
Definition 6309.c:7232
void cpu_float_single_mul(Environment *_environment, char *_x, char *_y, char *_result)
Definition 6309.c:7211
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_store_float(Environment *_environment, char *_destination, double _value)
Store a string to a variable.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * fp_cos(Environment *_environment, char *_angle)
Definition fp_cos.c:82
FloatType floatType
Definition ugbc.h:2400
FloatTypeAngle angle
Definition ugbc.h:869
FloatTypePrecision precision
Definition ugbc.h:991
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_FLOAT
Definition ugbc.h:522
@ FT_FAST
Definition ugbc.h:854
@ FT_SINGLE
Definition ugbc.h:855
@ FT_DEGREE
Definition ugbc.h:862