ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
sqr.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
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41extern char DATATYPE_AS_STRING[][16];
42
50/* <usermanual>
51@keyword SQR
52
53@english
54
55The ''SQR'' command (short for "square root") is a mathematical function that
56is used to calculate the square root of a number. In other words, given a number
57''x'', the command ''SQR(x)'' returns the number that, when multiplied by itself,
58gives the result ''x''.
59
60The ''SQR'' command has many applications in programming: it is used in any
61calculation that requires the calculation of the square root, such as in the
62Pythagorean theorem. The square root is present in many physics formulas,
63such as in the calculation of velocity. It is used also to calculate the
64lengths of segments or areas of geometric figures, and in many statistical
65calculations, such as in the calculation of standard deviation.
66
67The square root of a negative number is not defined in real numbers. If you
68try to calculate the square root of a negative number, the result is indefinite.
69Moreover, the function is defined in the integer numbers domain.
70
71@italian
72
73Il comando ''SQR'' (abbreviazione di "square root") è una funzione matematica
74utilizzata per calcolare la radice quadrata di un numero. In altre parole,
75dato un numero ''x'', il comando ''SQR(x)'' restituisce il numero che,
76moltiplicato per se stesso, dà il risultato ''x''.
77
78Il comando ''SQR'' ha molte applicazioni nella programmazione: viene utilizzato
79in qualsiasi calcolo che richieda il calcolo della radice quadrata, come nel
80teorema di Pitagora. La radice quadrata è presente in molte formule fisiche,
81come nel calcolo della velocità. Viene anche utilizzata per calcolare le
82lunghezze di segmenti o aree di figure geometriche e in molti calcoli statistici,
83come nel calcolo della deviazione standard.
84
85La radice quadrata di un numero negativo non è definita nei numeri reali.
86Se si tenta di calcolare la radice quadrata di un numero negativo,
87il risultato è indefinito. Inoltre, la funzione è definita nel dominio
88nei numeri interi.
89
90@syntax = SQR(expression)
91
92@example PRINT SQR(81)
93
94@usedInExample maths_sqr_01.bas
95
96</usermanual> */
97
98Variable * sqroot( Environment * _environment, char * _value ) {
99 Variable * value = variable_retrieve_or_define( _environment, _value, VT_WORD, 0 );
100
101 Variable * result = variable_temporary( _environment, VT_BYTE, "(result of SQR)");
102
103 switch( VT_BITWIDTH( value->type ) ) {
104 case 32:
106 case 16:
107 // if ( VT_SIGNED( value->type ) ) {
108 // CRITICAL_SQR_UNSUPPORTED( _value, DATATYPE_AS_STRING[value->type] );
109 // }
110 cpu_sqroot( _environment, value->realName, result->realName );
111 break;
112 case 8: {
113 Variable * value16 = variable_cast( _environment, value->name, VT_WORD );
114 cpu_sqroot( _environment, value16->realName, result->realName );
115 break;
116 }
117 case 0:
119 break;
120 }
121
122 return result;
123}
124
void cpu_sqroot(Environment *_environment, char *_number, char *_result)
Definition 6309.c:6113
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_cast(Environment *_environment, char *_source, VariableType _type)
Cast a variable from a type to another.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * sqroot(Environment *_environment, char *_value)
Return the square root of a variable.
Definition sqr.c:98
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
char * realName
Definition ugbc.h:982
#define CRITICAL_SQR_UNSUPPORTED(v, t)
Definition ugbc.h:3503
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_WORD
Definition ugbc.h:455
@ VT_BYTE
Definition ugbc.h:450
#define VT_BITWIDTH(t)
Definition ugbc.h:595
#define CRITICAL_SGN_UNSUPPORTED(v, t)
Definition ugbc.h:3476
char DATATYPE_AS_STRING[][16]