Math

Descrition del contenete del págine

Conversion de Math a pluri lingues de programation.

Etiquettes:

Ti-ci programa esset convertet a 23 lingues de programation.

Originale

Origin of math.bas:

Example included in Vintage BASIC 1.0.3.

http://vintage-basic.net/

10 INPUT"ENTER A NUMBER";N
20 ?"ABS(N)=";ABS(N)
25 ?"ATN(N)=";ATN(N)
30 ?"COS(N)=";COS(N)
40 ?"EXP(N)=";EXP(N)
50 ?"INT(N)=";INT(N)
60 ?"LOG(N)=";LOG(N)
70 ?"SGN(N)=";SGN(N)
80 ?"SQR(N)=";SQR(N)
90 ?"TAN(N)=";TAN(N)

In 8th

\ Math

\ Original version in BASIC:
\   Example included in Vintage BASIC 1.0.3.
\   http://www.vintage-basic.net

\ This version in 8th:
\   Copyright (c) 2023, Marcos Cruz (programandala.net)
\   SPDX-License-Identifier: Fair

\ Written on 2023-03-09.

\ Last modified 20251204T2247+0100.

32 constant number_max_len

: input \ -- n
  null
  repeat drop
    "Enter a number" .
    number_max_len null con:accept >n
    null? while! ;
  \ Ask the user to enter a number. If the entry cannot be converted to a number,
  \ ask again.

: function \ n w s --
  . "(" . 2 pick . ") = " . w:exec . cr ;
  \ Print the BASIC function in string _s_ and its result, which is obtained
  \ by executing word _w_ with parameter _n_.

: app:main \ --
  input
  dup ' n:abs  "ABS" function
  dup ' n:atan "ATN" function
  dup ' n:cos  "COS" function
  dup ' n:exp  "EXP" function
  dup ' n:int  "INT" function
  dup ' n:ln   "LOG" function
  dup ' n:sgn  "SGN" function
  dup ' n:sqrt "SQR" function
      ' n:tan  "TAN" function
  ;

In Ada

-- Math

-- Original version in BASIC:
--      Example included in Vintage BASIC 1.0.3.
--      http://www.vintage-basic.net

-- This version in Ada:
--      Copyright (c) 2025, Marcos Cruz (programandala.net)
--      SPDX-License-Identifier: Fair

-- Written on 2025-05-13, 2025-07-28.

-- Last modified: 20251204T2259+0100.

with ada.numerics.elementary_functions;
use ada.numerics.elementary_functions;

with ada.text_io;
use ada.text_io;

with ada.float_text_io;

procedure math is

    n : float;
    result : float;

begin

    n := 0.0;

    put ("Enter a number: ");
    ada.float_text_io.get (n);

    result := abs n;
    put_line ("ABS($n) --> abs $n --> " & result'image);
    put_line ("ATN($n) --> arctan ($n) --> " & arctan (n)'image);
    put_line ("COS($n) --> cos ($n) --> " & cos (n)'image);
    put_line ("EXP($n) --> exp ($n) --> " & exp (n)'image);

    put_line ("INT($n) --> integer ($n) --> " & integer (n)'image);

    if n > 0.0 then
        put_line ("LOG($n) --> log ($n) --> " & log (n)'image);
    else
        put_line ("LOG($n) --> log ($n) --> undefined");
    end if;

    put_line (
        "SGN($n) --> integer (float'copy_sign (1.0, $n)) --> "
        & integer (float'copy_sign (1.0, n))'image
    );

    if n >= 0.0 then
        put_line ("SQR($n) --> sqrt ($n) --> " & sqrt (n)'image);
    else
        put_line ("SQR($n) --> sqrt ($n) --> math domyain error");
    end if;

    put_line ("TAN($n) --> tan ($n) --> " & tan (n)'image);

end math;

In Arturo

; Math

; Original version in BASIC:
;   Example included in Vintage BASIC 1.0.3.
;   http://www.vintage-basic.net

; This version in Arturo:
;   Copyright (c) 2023, Marcos Cruz (programandala.net)
;   SPDX-License-Identifier: Fair
;
; Written on 2023-10-18.
;
; Last modified: 20251205T0047+0100.

until [
    s: input "Enter a number: "
    dup numeric? s else [ print "Number expected. Retry." ]
] []
n: to :floating s

print ~"ABS(|n|) -> abs |n| -> |abs n|"
print ~"ATN(|n|) -> atan |n| -> |atan n|"
print ~"COS(|n|) -> cos |n| -> |cos n|"
print ~"EXP(|n|) -> exp |n| -> |exp n|"
print ~"INT(|n|) -> floor |n| -> |floor n|"
print ~"LOG(|n|) -> ln |n| -> |ln n|"
print ~"SGN(|n|) -> if? less? |n| 0 [neg 1] else [1] -> |if? less? n 0 [neg 1] else [1]|"
print ~"SQR(|n|) -> sqrt |n| -> |sqrt n|"
print ~"TAN(|n|) -> tan |n| -> |tan n|"

In C#

// Math

// Original version in BASIC:
//  Example included in Vintage BASIC 1.0.3.
//  http://www.vintage-basic.net

// This version in C#:
//  Copyright (c) 2024, Marcos Cruz (programandala.net)
//  SPDX-License-Identifier: Fair
//
// Written on 2024-12-21.
//
// Last modified: 20251205T1540+0100.

using System;

class vintageBasicMath
{
    static void Main()
    {
        double n;

        while (true)
        {
            Console.Write("Enter a number: ");
            try
            {
                // XXX TODO Support any culture-specific number notation.
                n = Double.Parse(Console.ReadLine());
                break;
            }
            catch
            {
            }
        }

        Console.WriteLine($"ABS({n}) -> Math.Abs({n}) -> {Math.Abs(n)}");
        Console.WriteLine($"ATN({n}) -> Math.Atan({n}) -> {Math.Atan(n)}");
        Console.WriteLine($"COS({n}) -> Math.Cos({n}) -> {Math.Cos(n)}");
        Console.WriteLine($"EXP({n}) -> Math.Exp({n}) -> {Math.Exp(n)}");
        Console.WriteLine($"INT({n}) -> (int) {n}) -> {(int) n}");
        Console.WriteLine($"LOG({n}) -> Math.Log({n}, Math.E) -> {Math.Log(n, Math.E)}");
        Console.WriteLine($"SGN({n}) -> Math.Sign({n}) -> {Math.Sign(n)}");
        Console.WriteLine($"SQR({n}) -> Math.Sqrt({n}) -> {Math.Sqrt(n)}");
        Console.WriteLine($"TAN({n}) -> Math.Tan({n}) -> {Math.Tan(n)}");
    }
}

In Chapel

// Math

// Original version in BASIC:
//     Example included in Vintage BASIC 1.0.3.
//     http://www.vintage-basic.net

// This version in Chapel:
//     Copyright (c) 2025, Marcos Cruz (programandala.net)
//     SPDX-License-Identifier: Fair
//
// Written on 2025-04-05
//
// Last modified: 20250405T2337+0200.

import IO;
import Math;

proc acceptString(prompt: string): string {
    write(prompt);
    IO.stdout.flush();
    return IO.readLine().strip();
}

proc acceptValidReal(prompt: string): real {
    var result: real;
    while true {
        var s: string = acceptString(prompt);
        try {
            result = s: real;
            break;
        }
        catch exc {
            writeln("Real number expected.");
        }
    }
    return result;
}

proc main() {

    var n: real = acceptValidReal("Enter a number: ");

    writef("ABS(%r) -> Math.abs(%r) -> %r\n", n, n, Math.abs(n));
    writef("ATN(%r) -> Math.atan(%r) -> %r\n", n, n, Math.atan(n));
    writef("COS(%r) -> Math.cos(%r) -> %r\n", n, n, Math.cos(n));
    writef("EXP(%r) -> Math.exp(%r) -> %r\n", n, n, Math.exp(n));
    writef("INT(%r) -> %r: int -> %i\n", n, n, n: int);
    writef("LOG(%r) -> Math.log(%r) -> %r\n", n, n, Math.log(n));
    writef("SGN(%r) -> Math.sgn(%r)) -> %i\n", n, n, Math.sgn(n));
    writef("SQR(%r) -> Math.sqrt(%r) -> %r\n", n, n, Math.sqrt(n));
    writef("TAN(%r) -> Math.tan(%r) -> %r\n", n, n, Math.tan(n));

}

In Crystal

# Math

# Original version in BASIC:
#     Example included in Vintage BASIC 1.0.3.
#     http://www.vintage-basic.net

# This version in Crystal:
#     Copyright (c) 2023, 2025, Marcos Cruz (programandala.net)
#     SPDX-License-Identifier: Fair
#
# Written in 2023-09, 2025-05.
#
# Last modified: 20250513T0055+0200.

while true
  puts "Enter a number: "
  begin
    n = gets.not_nil!.to_f
    break
  rescue
    puts "That wasn't a valid number."
  end
end

puts "ABS(#{n}) --> #{n}.abs --> #{n.abs}"
puts "ATN(#{n}) --> Math.atan(#{n}) --> #{Math.atan(n)}"
puts "COS(#{n}) --> Math.cos(#{n}) --> #{Math.cos(n)}"
puts "EXP(#{n}) --> Math.exp(#{n}) --> #{Math.exp(n)}"
puts "INT(#{n}) --> #{n}.to_i --> #{n.to_i}"
puts "LOG(#{n}) --> Math.log(#{n}) --> #{Math.log(n)}"
puts "SGN(#{n}) --> #{n}.sign --> #{n.sign}"
puts "SQR(#{n}) --> Math.sqrt(#{n}) --> #{Math.sqrt(n)}"
puts "TAN(#{n}) --> Math.tan(#{n}) --> #{Math.tan(n)}"

In D

// Math

// Original version in BASIC:
//     Example included in Vintage BASIC 1.0.3.
//     http://www.vintage-basic.net

// This version in D:
//     Copyright (c) 2025, Marcos Cruz (programandala.net)
//     SPDX-License-Identifier: Fair
//
// Written on 2025-03-22.
//
// Last modified: 20251219T2147+0100.

module math;

string acceptString(string prompt)
{
    import std.stdio : readln;
    import std.stdio : write;
    import std.string : strip;

    write(prompt);
    return strip(readln());
}

float acceptValidFloat(string prompt)
{
    import std.conv : to;
    import std.stdio : writeln;

    float result;
    while (true)
    {
        string s = acceptString(prompt);
        try
        {
            result = to!float(s);
            break;
        }
        catch (Exception exc)
        {
            writeln("Real number expected.");
        }
    }
    return result;
}


void main()
{
    import std.conv : to;
    import std.math : abs;
    import std.math : atan;
    import std.math : cos;
    import std.math : exp;
    import std.math : log;
    import std.math : sgn;
    import std.math : sqrt;
    import std.math : tan;
    import std.stdio : writefln;

    float n = acceptValidFloat("Enter a number: ");

    writefln("ABS(%1$f) -> abs(%1$f) -> %2$f", n, abs(n));
    writefln("ATN(%1$f) -> atan(%1$f) -> %2$f", n, atan(n));
    writefln("COS(%1$f) -> cos(%1$f) -> %2$f", n, cos(n));
    writefln("EXP(%1$f) -> exp(%1$f) -> %2$f", n, exp(n));
    writefln("INT(%1$f) -> to!int(%1$f) -> %2$d", n, to!int(n));
    writefln("LOG(%1$f) -> log(%1$f) -> %2$f", n, log(n));
    writefln("SGN(%1$f) -> to!int(sgn(%1$f)) -> %2$d", n, to!int(sgn(n)));
    writefln("SQR(%1$f) -> sqrt(%1$f) -> %2$f", n, sqrt(n));
    writefln("TAN(%1$f) -> tan(%1$f) -> %2$f", n, tan(n));

}

In Go

/*
Math

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Go:
    Copyright (c) 2024, 2025, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2024-12-29, 2025-03-22.

Last modified: 20250322T2328+0100.
*/

package main

import "fmt"
import "math"
import "strconv"

func inputString(prompt string) string {

    fmt.Print(prompt)
    s := ""
    fmt.Scanf("%s", &s)
    return s

}

func inputFloat(prompt string) float64 {

    var float float64
    var err error
    for {
        float, err = strconv.ParseFloat(inputString(prompt), 64)
        if err == nil {
            break
        } else {
            fmt.Println("Real number expected.")
        }
    }
    return float

}

func Sign(a float64) int {
    switch {
    case a < 0:
        return -1
    case a > 0:
        return +1
    }
    return 0
}

func main() {

    var n float64 = inputFloat("Enter a number: ")

    fmt.Printf("ABS(%[1]v) -> math.Abs(%[1]v) -> %[2]v\n", n, math.Abs(n))
    fmt.Printf("ATN(%[1]v) -> math.Atan(%[1]v) -> %[2]v\n", n, math.Atan(n))
    fmt.Printf("COS(%[1]v) -> math.Cos(%[1]v) -> %[2]v\n", n, math.Cos(n))
    fmt.Printf("EXP(%[1]v) -> math.Exp(%[1]v) -> %[2]v\n", n, math.Exp(n))
    fmt.Printf("INT(%[1]v) -> int(%[1]v) -> %[2]v\n", n, int(n))
    fmt.Printf("LOG(%[1]v) -> math.Log(%[1]v) -> %[2]v\n", n, math.Log(n))
    fmt.Printf("SGN(%[1]v) -> Sign(%[1]v) /* ad hoc */ -> %[2]v\n", n, Sign(n))
    fmt.Printf("SQR(%[1]v) -> math.Sqrt(%[1]v) -> %[2]v\n", n, math.Sqrt(n))
    fmt.Printf("TAN(%[1]v) -> math.Tan(%[1]v) -> %[2]v\n", n, math.Tan(n))

}

In Hare

// Math
//
// Original version in BASIC:
//      Example included in Vintage BASIC 1.0.3.
//      http://www.vintage-basic.net
//
// This version in Hare:
//      Copyright (c) 2025, Marcos Cruz (programandala.net)
//      SPDX-License-Identifier: Fair
//
// Written in 2025-02.
//
// Last modified: 20260213T1645+0100.

use bufio;
use fmt;
use math;
use os;
use strconv;
use strings;

fn print_prompt(prompt: str = "") void = {
        fmt::print(prompt)!;
        bufio::flush(os::stdout)!;
};

fn accept_string(prompt: str = "") str = {
        print_prompt(prompt);
        const buffer = match (bufio::read_line(os::stdin)!) {
                case let buffer: []u8 =>
                        yield buffer;
                case =>
                        return "";
                };
        defer free(buffer);
        return strings::dup(strings::fromutf8(buffer)!)!;
};

fn accept_f64(prompt: str = "") (f64 | ...strconv::error) = {
        const s: str = accept_string(prompt);
        defer free(s);
        return strconv::stof64(s);
};

fn accept_valid_f64(prompt: str = "", error: str = "Real number expected.") f64 = {
        for (true) {
                match (accept_f64(prompt)) {
                case let n: f64 =>
                        return n;
                case =>
                        fmt::println(error)!;
                };
        };
};

export fn main() void = {
        fmt::println()!;
        const n: f64 = accept_valid_f64("Enter a number: ");

        fmt::printfln("ABS({0}) -> math::abs({0}) -> {1}", n, math::absf64(n))!;
        fmt::printfln("ATN({0}) -> math::atan({0}) -> {1}", n, math::atanf64(n))!;
        fmt::printfln("COS({0}) -> math::cos_f64({0}) -> {1}", n, math::cosf64(n))!;
        fmt::printfln("EXP({0}) -> math::expf64({0}) -> {1}", n, math::expf64(n))!;
        fmt::printfln("INT({0}) -> ({0}): int -> {1}", n, n: int)!;
        fmt::printfln("LOG({0}) -> math::log_f64({0}) -> {1}", n, math::logf64(n))!;
        fmt::printfln(
                "SGN({0}) -> if ({0} == 0.0) 0 else math::sign_f64({0}) -> {1}",
                n,
                if (n == 0.0) 0 else math::signf64(n)
        )!;
        fmt::printfln("SQR({0}) -> math::sqrt_f64({0}) -> {1}", n, math::sqrtf64(n))!;
        fmt::printfln("TAN({0}) -> math::tan_f64({0}) -> {1}", n, math::tanf64(n))!;
};

In Icon

# Math
#
# Original version in BASIC:
#   Example included in Vintage BASIC 1.0.3.
#   http://www.vintage-basic.net
#
# This version in Icon:
#   Copyright (c) 2023, Marcos Cruz (programandala.net)
#   SPDX-License-Identifier: Fair
#
# Written in 2023-03, 2023-09.
#
# Last modified 20231103T2342+0100.

link numbers # sign(), trunc()

procedure main()

    writes("Enter a number ")
    n := read()

    write("ABS(", n, ") --> abs(", n, ") --> ", abs(n))
    write("ATN(", n, ") --> atan(", n, ") --> ", atan(n))
    write("COS(", n, ") --> cos(", n, ") --> ", cos(n))
    write("EXP(", n, ") --> exp(", n, ") --> ", exp(n))
    write("INT(", n, ") --> trunc(", n, ") --> ", trunc(n))
    write("LOG(", n, ") --> log(", n, ") --> ", log(n))
    write("SGN(", n, ") --> sign(", n, ") --> ", sign(n))
    write("SQR(", n, ") --> sqrt(", n, ") --> ", sqrt(n))
    write("TAN(", n, ") --> tan(", n, ") --> ", tan(n))

end

In Janet

# Math

# Original version in BASIC:
#  Example included in Vintage BASIC 1.0.3.
#  http://www.vintage-basic.net

# This version in Janet:
#  Copyright (c) 2025, Marcos Cruz (programandala.net)
#  SPDX-License-Identifier: Fair
#
# Written on 2025-12-24.
#
# Last modified 20251225T1945+0100.

(defn accept-string [prompt-text]
  (prin prompt-text)
  (string/trim (getline)))

(defn accept-number [prompt-text]
  (def input (accept-string prompt-text))
  (def number (scan-number input))
  (if number
    number
    (do
      (print "Number expected.")
      (accept-number prompt-text))))

(defn show [basic-function scheme-procedure n result]
  (print basic-function "(" n ") -> (" scheme-procedure " " n ") -> " result))

(defn sign [n]
  (cond
    (< n 0) -1
    (> n 0) 1
    0))

(defn main [& args]
  (def n (accept-number "Enter a number: "))
  (show "ABS" "math/abs" n (math/abs n))
  (show "ATN" "math/atan" n (math/atan n))
  (show "COS" "math/cos" n (math/cos n))
  (show "EXP" "math/exp" n (math/exp n))
  (show "INT" "math/trunc" n (math/trunc n))
  (show "LOG" "math/log" n (math/log n))
  (show "SGN" "ad-hoc/sign" n (sign n))
  (show "SQR" "math/sqrt" n (math/sqrt n))
  (show "TAN" "math/tan" n (math/tan n)))

In Julia

# Math

# Original version in BASIC:
#     Example included in Vintage BASIC 1.0.3.
#     http://www.vintage-basic.net

# This version in Julia:
#     Copyright (c) 2023, Marcos Cruz (programandala.net)
#     SPDX-License-Identifier: Fair

# Written in 2023-07, 2023-08, 2023-09, 2024-10.

# Last modified: 20241029T2117+0100.

n = 0.0

while true
    print("Enter a number: ")
    global n = tryparse(Float64, readline())
    if n == nothing
        println("Not a valid number.")
    else
        break
    end
end

println("ABS($n) --> abs($n) --> ", abs(n))
println("ATN($n) --> atan($n) --> ", atan(n))
println("COS($n) --> cos($n) --> ", cos(n))
println("EXP($n) --> exp($n) --> ", exp(n))
println("INT($n) --> floor(Int64, $n) --> ", floor(Int64, n))

if n >= 0
    println("LOG($n) --> log($n) --> ", log(n))
else
    println("LOG($n) --> log($n) --> undefined")
end

println("SGN($n) --> sign($n) --> ", sign(n))

if n >= 0
    println("SQR($n) --> sqrt($n) --> ", sqrt(n))
else
    println("SQR($n) --> sqrt($n) --> math domain error")
end

println("TAN($n) --> tan($n) --> ", tan(n))

In Kotlin

/*
Math

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Kotlin:
    Copyright (c) 2023, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2023-08, 2023-10, 2024-03, 2025-05.

Last modified: 20250519T0016+0200.
*/

fun main() {

    print("Enter a number: ")
    var input = readln()
    var n: Double = try { input.toDouble() } catch (e: NumberFormatException) { 0.0 }

    println("ABS($n) --> kotlin.math.abs($n) --> ${kotlin.math.abs(n)}")
    println("ATN($n) --> kotlin.math.atan($n) --> ${kotlin.math.atan(n)}")
    println("COS($n) --> kotlin.math.cos($n) --> ${kotlin.math.cos(n)}")
    println("EXP($n) --> kotlin.math.exp($n) --> ${kotlin.math.exp(n)}")
    println("INT($n) --> $n.toInt() --> ${n.toInt()}")
    println("LOG($n) --> kotlin.math.ln($n) --> ${kotlin.math.ln(n)}")
    println("SGN($n) --> kotlin.math.sign($n) --> ${kotlin.math.sign(n)}")
    println("SQR($n) --> kotlin.math.sqrt($n) --> ${kotlin.math.sqrt(n)}")
    println("TAN($n) --> kotlin.math.tan($n) --> ${kotlin.math.tan(n)}")

}

In Nim

#[
Math

Original version in BASIC:
  Example included in Vintage BASIC 1.0.3.
  http://www.vintage-basic.net

This version in Nim:
  Copyright (c) 2023, 2025, Marcos Cruz (programandala.net)
  SPDX-License-Identifier: Fair

Written on 2023-08-26, 2025-01-20.

Last modified: 20250731T1954+0200.
]#

import std/math
import std/strutils

var n: float
while true:
  try:
    write(stdout, "Enter a number: ")
    n = parseFloat(readLine(stdin))
    break
  except ValueError:
    writeLine(stdout, "Real number expected.")

echo "ABS(", n, ") = abs(", n, ") = ", abs(n)
echo "ATN(", n, ") = arctan(", n, ") = ", arctan(n)
echo "COS(", n, ") = cos(", n, ") = ", cos(n)
echo "EXP(", n, ") = exp(", n, ") = ", exp(n)
echo "INT(", n, ") = int(", n, ") = ", int(n)
echo "LOG(", n, ") = log(", n, ", E) = ", log(n, E)
echo "SGN(", n, ") = sgn(", n, ") = ", sgn(n)
echo "SQR(", n, ") = sqrt(", n, ") = ", sqrt(n)
echo "TAN(", n, ") = tan(", n, ") = ", tan(n)

In Odin

/*
Math

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Odin:
    Copyright (c) 2023, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2023-03, 2023-04, 2023-09, 2023-12.

Last modified: 20250101T2036+0100.
*/

package vintage_basic_math

import "../lib/anodino/src/read"
import "core:fmt"
import "core:math"

main :: proc() {

    n : f64
    ok := false
    for ; ! ok; n, ok = read.a_prompted_f64("Enter a number: ") { }

    fmt.printfln("ABS(%v) -> math.abs(%v) -> %v", n, n, math.abs(n))
    fmt.printfln("ATN(%v) -> math.atan(%v) -> %v", n, n, math.atan(n))
    fmt.printfln("COS(%v) -> math.cos_f64(%v) -> %v", n, n, math.cos_f64(n))
    fmt.printfln("EXP(%v) -> math.exp(%v) -> %v", n, n, math.exp(n))
    fmt.printfln("INT(%v) -> int(%v) -> %v", n, n, int(n))
    fmt.printfln("LOG(%v) -> math.log_f64(%v, %v) -> %v", n, n, math.E, math.log_f64(n, math.E))
    fmt.printfln("SGN(%v) -> math.sign_f64(%v) -> %v", n, n, math.sign_f64(n))
    fmt.printfln("SQR(%v) -> math.sqrt_f64(%v) -> %v", n, n, math.sqrt_f64(n))
    fmt.printfln("TAN(%v) -> math.tan_f64(%v) -> %v", n, n, math.tan_f64(n))

}

In Pike

#! /usr/bin/env pike

// Math

// Original version in BASIC:
//  Example included in Vintage BASIC 1.0.3.
//  http://www.vintage-basic.net

// This version in Pike:
//  Copyright (c) 2025, Marcos Cruz (programandala.net)
//  SPDX-License-Identifier: Fair
//
// Written on 2025-03-09.
//
// Last modified: 20250309T2257+0100.

string accept_string(string prompt) {
    write(prompt);
    return Stdio.stdin->gets();
}

float accept_float(string prompt) {
    // NB The casting accepts any valid float at the start of the string
    // and ignores the rest;  if no valid float is found at the start of
    // the string, zero is returned.
    return (float) accept_string(prompt);
}

float accept_valid_float(string prompt) {
    float n = 0;
    while (n <= 0) {
        n = accept_float(prompt);
        if (n <= 0) {
            write("Float greater than zero expected.\n");
        }
    }
    return n;
}


void main() {

    float n = accept_valid_float("Enter a number: ");

    write("ABS(%[0]f) -> abs(%[0]f) -> %[1]f\n", n, abs(n));
    write("ATN(%[0]f) -> atan(%[0]f) -> %[1]f\n", n, atan(n));
    write("COS(%[0]f) -> cos(%[0]f) -> %[1]f\n", n, cos(n));
    write("EXP(%[0]f) -> exp(%[0]f) -> %[1]f\n", n, exp(n));
    write("INT(%[0]f) -> (int) (%[0]f) -> %[1]d\n", n, (int) n);
    write("LOG(%[0]f) -> log(%[0]f) -> %[1]f\n", n, log(n));
    write("SGN(%[0]f) -> sgn(%[0]f) -> %[1]d\n", n, sgn(n));
    write("SQR(%[0]f) -> sqrt(%[0]f) -> %[1]f\n", n, sqrt(n));
    write("TAN(%[0]f) -> tan(%[0]f) -> %[1]f\n", n, tan(n));

}

In Python

# Math

# Original version in BASIC:
#     Example included in Vintage BASIC 1.0.3.
#     http://www.vintage-basic.net

# This version in Python:
#     Copyright (c) 2023, Marcos Cruz (programandala.net)
#     SPDX-License-Identifier: Fair
#
# Written on 2024-10-29.
#
# Last modified: 20241203T2230+0100.

import ast
import math

def sign(n):
    if n > 0:
        return 1
    elif n < 0:
        return -1
    else:
        return 0

while True:
    try:
        n = ast.literal_eval(input("Enter a number: "))
        break
    except:
        print("Not a valid number.")

print(f"ABS({n}) --> abs({n}) --> ", abs(n))
print(f"ATN({n}) --> math.atan({n}) --> ", math.atan(n))
print(f"COS({n}) --> math.cos({n}) --> ", math.cos(n))
print(f"EXP({n}) --> math.exp({n}) --> ", math.exp(n))
print(f"INT({n}) --> int({n}) --> ", int(n))

if n >= 0:
    print(f"LOG({n}) --> math.log({n}) --> ", math.log(n))
else:
    print(f"LOG({n}) --> math.log({n}) --> undefined")

print(f"SGN({n}) --> sign({n}) # custom function --> ", sign(n))

if n >= 0:
    print(f"SQR({n}) --> math.sqrt({n}) --> ", math.sqrt(n))
else:
    print(f"SQR({n}) --> math.sqrt({n}) --> math domain error")

print(f"TAN({n}) --> math.tan({n}) --> ", math.tan(n))

In Raku

# Math
#
# Original version in BASIC:
#   Example included in Vintage BASIC 1.0.3.
#   http://www.vintage-basic.net
#
# This version in Raku:
#   Copyright (c) 2024, Marcos Cruz (programandala.net)
#   SPDX-License-Identifier: Fair
#
# Written in 2024-12-03.
#
# Last modified: 20241203T1812+0100.

my $n;

loop {
    try {
        $n = +prompt 'Enter a number: ';
        CATCH {
            default {
                put 'Number expected.';
                next;
            }
        }
    }
    last;
}

put "ABS($n) -> abs($n) -> ", abs($n);
put "ATN($n) -> atan($n) -> ", atan($n);
put "COS($n) -> cos($n) -> ", cos($n);
put "EXP($n) -> exp($n) -> ", exp($n);
put "INT($n) -> floor($n) -> ", floor($n);
put "LOG($n) -> log($n) -> ", log($n);
put "SGN($n) -> sign($n) -> ", sign($n);
put "SQR($n) -> sqrt($n) -> ", sqrt($n);
put "TAN($n) -> tan($n) -> ", tan($n);

In Ring

/*
Math

Original version in BASIC:
Example included in Vintage BASIC 1.0.3.
http://www.vintage-basic.net

This version in Ring:
Copyright (c) 2023, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair

Written on 2024-03-18 and 2024-04-04.

Last modified: 20240404T1954+0200.
*/

load "stdlib.ring" // `sign()`

// If the given string is a valid number, i.e. it contains only digits and one
// optional dot, return `true`; otherwise return `false`.
func isValidNumber(s)
    decimalPointFound = false
    for n = 1 to len(s)
        if s[n] = "."
            if decimalPointFound
                return false
            else
                decimalPointFound = true
                continue
            end
        elseif not isDigit(s[n])
            if not (n = 1 and s[n] = "-")
                return false
            end
        end
    next
    return true
end

// Prompt the user for a valid number, check it and return it.
func validNumber
    while true
        put "Enter a number: "
        get n
        if isValidNumber(n)
            break
        else
            put "Number expected. Retry." + nl
        end
    end
    return 0 + n
end

func main

    n = validNumber()

    print("ABS(#{n}) --> fabs(#{n}) --> ")
    put fabs(n) + nl
    print("ATN(#{n}) --> atan(#{n}) --> ")
    put atan(n) + nl
    print("COS(#{n}) --> cos(#{n}) --> ")
    put cos(n) + nl
    print("EXP(#{n}) --> exp(#{n}) --> ")
    put exp(n) + nl
    print("INT(#{n}) --> floor(#{n}) --> ")
    put floor(n) + nl
    print("LOG(#{n}) --> log(#{n}) --> ")
    put log(n) + nl
    print("SGN(#{n}) --> sign(#{n}) --> ")
    put sign(n) + nl
    print("SQR(#{n}) --> sqrt(#{n}) --> ")
    put sqrt(n) + nl
    print("TAN(#{n}) --> tan(#{n}) --> ")
    put tan(n) + nl

end

In Scala

/*
Math

Original version in BASIC:
  Example included in Vintage BASIC 1.0.3.
  http://www.vintage-basic.net

This version in Scala:
  Copyright (c) 2023, Marcos Cruz (programandala.net)
  SPDX-License-Identifier: Fair

Written on 2023-08-26.

Last modified: 20231103T2352+0100.
*/

@main def main() =

  print("Enter a number: ")
  var n : Float =
  try
    io.StdIn.readFloat()
  catch
    case e: NumberFormatException => 0.0

  print(s"ABS($n) = math.abs($n) = "); println(math.abs(n))
  print(s"ATN($n) = math.atan($n) = "); println(math.atan(n))
  print(s"COS($n) = math.cos($n) = "); println(math.cos(n))
  print(s"EXP($n) = math.exp($n) = "); println(math.exp(n))
  print(s"INT($n) = $n.toInt = "); println(n.toInt)
  print(s"LOG($n) = math.log($n) = "); println(math.log(n))
  print(s"SGN($n) = math.signum($n) = "); println(math.signum(n))
  print(s"SQR($n) = math.sqrt($n) = "); println(math.sqrt(n))
  print(s"TAN($n) = math.tan($n) = "); println(math.tan(n))

In Scheme

; Math

; Original version in BASIC:
;   Example included in Vintage BASIC 1.0.3.
;   http://www.vintage-basic.net

; This version in Scheme (Bigloo):
;   Copyright (c) 2025, Marcos Cruz (programandala.net)
;   SPDX-License-Identifier: Fair
;
; Written on 2025-12-04.
;
; Last modified 20251224T1230+0100.

(module math)

(define (accept-string prompt)
    (display prompt)
    (read-line))

(define (accept-number prompt)
    (let ((input (accept-string prompt)))
        (let ((number (string->number input)))
            (if number
                number
                (begin
                    (display "Number expected.")
                    (newline)
                    (accept-number prompt))))))

(define (show basic-function scheme-procedure n result)
    (display
      (format "~a(~d) -> (~a ~d) -> ~d" basic-function n scheme-procedure n result))
    (newline))

(define (sign n)
    (cond
        ((< n 0) -1)
        ((> n 0) 1)
        (else 0)))

(define n (accept-number "Enter a number: "))

(show "ABS" "abs" n (abs n))
(show "ATN" "atan" n (atan n))
(show "COS" "cos" n (cos n))
(show "EXP" "exp" n (exp n))
(show "INT" "truncate" n (truncate n))
(show "LOG" "log" n (log n))
(show "SGN" "ad-hoc:sign" n (sign n))
(show "SQR" "sqrt" n (sqrt n))
(show "TAN" "tan" n (tan n))

In Swift

/*
Math

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Swift:
    Copyright (c) 2025, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2025-04-23.

Last modified: 20250423T1508+0200.
*/

import Foundation

let n: Double

while true {
    print("Enter a number: ", terminator: "")
    if let input = readLine(), let number = Double(input) {
        n = number
        break
    } else {
        print("Number expected.")
    }
}

func sign (_: Double) -> Int {
    switch true {
        case n < 0: return -1
        case n > 0: return 1
        default: return 0
    }
}

print("ABS(\(n)) --> abs(\(n)) --> \(abs(n))")
print("ATN(\(n)) --> atan(\(n)) --> \(atan(n))")
print("COS(\(n)) --> cos(\(n)) --> \(cos(n))")
print("EXP(\(n)) --> exp(\(n)) --> \(exp(n))")
print("INT(\(n)) --> Int(\(n)) --> \(Int(n))")
print("LOG(\(n)) --> log2(\(n)) --> \(log(n))")
print("SGN(\(n)) --> \(n).sign == .minus ? -1 : +1 --> \(n.sign == .minus ? -1 : 1)")
print("SGN(\(n)) --> sign(\(n)) --> \(sign(n)) // ad hoc function")
print("SQR(\(n)) --> sqrt(\(n)) --> \(sqrt(n))")
print("TAN(\(n)) --> tan(\(n)) --> \(tan(n))")

In V

/*
Math

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in V:
    Copyright (c) 2025, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2025-01-03.

Last modified: 20250103T1730+0100.
*/
import math
import os
import strconv

fn input_float(prompt string) f64 {
    mut n := 0.0
    for {
        n = strconv.atof64(os.input(prompt)) or { continue }
        break
    }
    return n
}

fn main() {
    n := input_float('Enter a number: ')
    println('ABS(${n}) -> math.abs(${n}) -> ${math.abs(n)}')
    println('ATN(${n}) -> math.atan(${n}) -> ${math.atan(n)}')
    println('COS(${n}) -> math.cos(${n}) -> ${math.cos(n)}')
    println('EXP(${n}) -> math.exp(${n}) -> ${math.exp(n)}')
    println('INT(${n}) -> int(${n}) -> ${int(n)}')
    println('LOG(${n}) -> math.log(${n}) -> ${math.log(n)}')
    println('SGN(${n}) -> math.sign(${n}) -> ${math.sign(n)}')
    println('SQR(${n}) -> math.sqrt(${n}) -> ${math.sqrt(n)}')
    println('TAN(${n}) -> math.tan(${n}) -> ${math.tan(n)}')
}

Págines relatet

Basics off
Metaprojecte pri li projectes «Basics of…».

Extern ligamentes relatet