First commit of partial RosettaCode contents.

Pushing this for testing purposes. Lots of work still needed.
This commit is contained in:
Ingy döt Net 2013-04-08 13:02:41 -07:00
commit 1e05ecd7ee
781 changed files with 9080 additions and 0 deletions

View file

@ -0,0 +1,5 @@
Write a function which tests if infinity is supported for floating point numbers (this step should be omitted for languages where the language specification already demands the existence of infinity, e.g. by demanding [[IEEE]] numbers), and if so, returns positive infinity. Otherwise, return the largest possible positive floating point number.
For languages with several floating point types, use the type of the literal constant 1.5 as floating point type.
C.F. [[Extreme floating point values]]

4
Task/Infinity/1META.yaml Normal file
View file

@ -0,0 +1,4 @@
---
category:
- Discrete math
note: Basic language learning

View file

@ -0,0 +1,6 @@
BEGIN {
k=1;
while (2^(k-1) < 2^k) k++;
INF = 2^k;
print INF;
}

View file

@ -0,0 +1,9 @@
#define _ISOC99_SOURCE
#include <math.h>
#include <stdio.h>
int main() {
printf("%g\n", INFINITY);
return 0;
}

View file

@ -0,0 +1,11 @@
#include <math.h> /* HUGE_VAL */
#include <stdio.h> /* printf() */
double inf(void) {
return HUGE_VAL;
}
int main() {
printf("%g\n", inf());
return 0;
}

View file

@ -0,0 +1,2 @@
Number.POSITIVE_INFINITY
Number.NEGATIVE_INFINITY

View file

@ -0,0 +1 @@
isFinite x

View file

@ -0,0 +1 @@
Infinity

View file

@ -0,0 +1,7 @@
: inf ( -- f ) 1e 0e f/ ;
inf f. \ implementation specific. GNU Forth will output "inf"
: inf? ( f -- ? ) s" MAX-FLOAT" environment? drop f> ;
\ IEEE infinity is the only value for which this will return true
: has-inf ( -- ? ) ['] inf catch if false else inf? then ;

View file

@ -0,0 +1,2 @@
real :: x
real :: huge_real = huge(x)

View file

@ -0,0 +1,18 @@
program to_f_the_ineffable
use, intrinsic :: ieee_arithmetic
integer :: i
real dimension(2) :: y, x = (/ 30, ieee_value(y,ieee_positive_inf) /)
do i = 1, 2
if (ieee_support_datatype(x(i))) then
if (ieee_is_finite(x(i))) then
print *, 'x(',i,') is finite'
else
print *, 'x(',i,') is infinite'
end if
else
print *, 'x(',i,') is not in an IEEE-supported format'
end if
end do
end program to_f_the_ineffable

View file

@ -0,0 +1,19 @@
package main
import (
"fmt"
"math"
)
// function called for by task
func posInf() float64 {
return math.Inf(1) // argument specifies positive infinity
}
func main() {
x := 1.5 // type of x determined by literal
// that this compiles demonstrates that PosInf returns same type as x,
// the type specified by the task.
x = posInf() // test function
fmt.Println(x, math.IsInf(x, 1)) // demonstrate result
}

View file

@ -0,0 +1,4 @@
*Main> infinity :: Float
Infinity
*Main> infinity :: Double
Infinity

View file

@ -0,0 +1,4 @@
Prelude> 1 / 0 :: Float
Infinity
Prelude> 1 / 0 :: Double
Infinity

View file

@ -0,0 +1,4 @@
Prelude> read "Infinity" :: Float
Infinity
Prelude> read "Infinity" :: Double
Infinity

View file

@ -0,0 +1,8 @@
maxRealFloat :: RealFloat a => a -> a
maxRealFloat x = encodeFloat b (e-1) `asTypeOf` x where
b = floatRadix x - 1
(_,e) = floatRange x
infinity :: RealFloat a => a
infinity = if isInfinite inf then inf else maxRealFloat 1.0 where
inf = 1/0

View file

@ -0,0 +1,3 @@
public static double getInf(){
return Double.POSITIVE_INFINITY;
}

View file

@ -0,0 +1 @@
double biggestNumber = Double.MAX_VALUE;

View file

@ -0,0 +1,2 @@
double infinity = Double.POSITIVE_INFINITY; //defined as 1.0/0.0
Double.isInfinite(infinity); //true

View file

@ -0,0 +1,2 @@
Number.POSITIVE_INFINITY
Number.NEGATIVE_INFINITY

View file

@ -0,0 +1 @@
isFinite(x)

View file

@ -0,0 +1 @@
Infinity

View file

@ -0,0 +1,3 @@
function infinity()
return 1/0 --lua uses unboxed C floats for all numbers
end

View file

@ -0,0 +1 @@
INF

View file

@ -0,0 +1,2 @@
my $x = 0 - "inf";
my $y = 0 + "-inf";

View file

@ -0,0 +1 @@
my $x = "inf";

View file

@ -0,0 +1 @@
my $x = 1e1000;

View file

@ -0,0 +1 @@
my $y = 10**10**10;

View file

@ -0,0 +1,3 @@
use bigint;
my $x = 1e1000;
my $y = 10**10**10;

View file

@ -0,0 +1,3 @@
use bigint;
my $x = inf;
my $y = -inf;

View file

@ -0,0 +1,2 @@
my $x = 0 + "inf";
my $y = 0 + "+inf";

View file

@ -0,0 +1,4 @@
(load "@lib/math.l")
: (exp 1000.0)
-> T

View file

@ -0,0 +1,4 @@
>>> 1.0 / 0.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division

View file

@ -0,0 +1,2 @@
>>> float('infinity')
inf

View file

@ -0,0 +1 @@
a = Float::INFINITY # => Infinity

View file

@ -0,0 +1,10 @@
a = 1.0/0 # => Infinity
a.finite? # => false
a.infinite? # => 1
a = -1/0.0 # => -Infinity
a.infinite? # => -1
a = Float::MAX # => 1.79769313486232e+308
a.finite? # => true
a.infinite? # => nil

View file

@ -0,0 +1,11 @@
scala> 1 / 0.
res2: Double = Infinity
scala> -1 / 0.
res3: Double = -Infinity
scala> 1 / Double.PositiveInfinity
res4: Double = 0.0
scala> 1 / Double.NegativeInfinity
res5: Double = -0.0

View file

@ -0,0 +1,11 @@
% format %lx -1 ;# all bits set
ffffffffffffffff
% regsub f 0x[format %lx -1] 7 ;# unset the sign bit for positive
0x7fffffffffffffff
% set ii [expr [regsub f 0x[format %lx -1] 7]] ;# show as decimal
9223372036854775807
% incr ii
9223372036854775808 ;# silently upgrade to unbounded integer, still positive

View file

@ -0,0 +1,6 @@
package require Tcl 8.5
expr {1.0 / 0} ;# ==> Inf
expr {-1.0 / 0} ;# ==> -Inf
expr {inf} ;# ==> Inf
expr {1 / 0} ;# ==> "divide by zero" error; Inf not part of range of integer division