StdReal.icl 5.2 KB
Newer Older
1 2 3
implementation module StdReal

// ********************************************************
4 5
//	Concurrent Clean Standard Library Module Version 3.0
//	Copyright 2019 University of Nijmegen
6 7 8 9
// ********************************************************
import StdClass
import StdOverloaded,StdInt,StdArray
from StdBool import &&,||,not
10
from StdChar import instance == Char, instance < Char, class ==(..)
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

instance + Real
where
 (+) :: !Real !Real -> Real
 (+) a b
	= code inline {
			addR
	}

instance - Real
where
 (-) :: !Real !Real -> Real
 (-) a b
	= code inline {
			subR
	}

instance zero Real
where
 zero:: Real
 zero
	= code inline {
			pushR 0.0
	}

instance * Real
where
 (*) :: !Real !Real -> Real
 (*) a b
	= code inline {
			mulR
	}

instance / Real
where
 (/) :: !Real !Real -> Real
 (/) a b
	= code inline {
			divR
	}

instance one Real
where
 one:: Real
 one
	= code inline {
			pushR 1.0
	}

instance ^ Real
where
 (^) :: !Real !Real -> Real
 (^) a b
	= code inline {
			powR
	}

instance abs Real
where
 abs::!Real -> Real
 abs x
	= code inline {
			absR
	}

instance sign Real
where
 sign::!Real -> Int
 sign x | x == 0.0	= 0
		| x < 0.0	= -1
 					= 1

instance ~ Real
where
 ~ :: !Real -> Real
 ~ x
	= code inline {
			negR
	}

instance == Real
where
 (==) :: !Real !Real -> Bool
 (==) a b
	= code inline {
			eqR
	}

instance < Real
where
 (<) :: !Real !Real -> Bool
 (<) a b
	= code inline {
			 ltR
	}

instance ln Real
where
109
	ln :: !Real -> Real
110 111 112 113 114 115 116
	ln a
		= code inline {
				lnR
		}

instance log10 Real
where
117
	log10 :: !Real -> Real
118 119 120 121 122 123
	log10 a
		= code inline {
				log10R
		}

instance exp  Real
124 125 126
where
	exp :: !Real -> Real
	exp a
127 128 129 130 131
		= code inline {
				expR
		}

instance sqrt Real
132 133 134
where
	sqrt :: !Real -> Real
	sqrt a
135 136 137 138 139 140
		= code inline {
				sqrtR
		}

instance sin Real
where
141
	sin :: !Real -> Real
142 143 144 145 146 147 148
	sin a
		= code inline {
				sinR
		}

instance cos Real
where
149
	cos :: !Real -> Real
150 151 152 153 154 155
	cos a
		= code inline {
				cosR
		}

instance tan Real
156 157
where
	tan :: !Real -> Real
158 159 160 161 162 163 164
	tan a
		= code inline {
				tanR
		}
		
instance asin Real
where
165
	asin :: !Real -> Real
166 167 168 169 170 171 172
	asin a
		= code inline {
				asinR
		}

instance acos Real
where
173
	acos :: !Real -> Real
174 175 176 177 178 179 180
	acos a
		= code inline {
				acosR
		}

instance atan Real
where
181
	atan :: !Real -> Real
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	atan a
		= code inline {
				atanR
		}

instance sinh Real
where
	sinh x = (exp x - exp (~ x)) * 0.5

instance cosh Real
where
	cosh x =  (exp x + exp (~ x)) * 0.5

instance tanh Real
where
	tanh x = (expx - expmx) / (expx + expmx)
	where
		expx = exp x
		expmx = exp (~ x)

instance asinh Real
where
	asinh x = ln (x + sqrt (x*x + 1.0))

instance acosh Real
where
	acosh x = ln (x + sqrt (x*x - 1.0))  // only the positive value is taken

instance atanh Real
where
	atanh x = ln ((1.0 + x)/(1.0 - x)) * 0.5

instance toReal	Int
where
 toReal :: !Int -> Real
 toReal a
	= code inline {
			ItoR
	}

instance toReal Real
where
 toReal :: !Real -> Real
 toReal a
	= code inline {
			 no_op
	}

instance fromReal Int
where
 fromReal :: !Real -> Int
 fromReal a
	= code inline {
			RtoI
	}

instance fromReal Real
where
 fromReal :: !Real -> Real
 fromReal a
	= code inline {
			 no_op
	}

instance fromReal {#Char}
where
 fromReal :: !Real -> {#Char}
 fromReal a
	= code inline {
Camil Staps's avatar
Camil Staps committed
251
		.d 0 1 r
252 253 254 255 256 257
			jsr RtoAC
		.o 1 0
	}

instance toReal {#Char}
where
Camil Staps's avatar
Camil Staps committed
258
 toReal :: !{#Char} -> Real
259 260 261 262 263 264 265 266 267 268
 toReal s
	|	len == 0
			=	0.0
	|	first  == '-'
			= 	~ signedval
	|	first  == '+'
			=	signedval
	//	otherwise
			= 	val
 where
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	len = size s
	first = s.[0]
	signedval = toReal1 s 1 0.0
	val = toReal1 s 0 0.0

	toReal1 :: !{#Char} !Int !Real -> Real
	toReal1 s posn val
		| posn == len
			= 	val
		| digit
			= 	toReal1 s (posn+1) (toReal n + 10.0*val)
		| c == '.'
			= 	toRealWithDot s (posn+1) val 0
		| c== 'e' || c== 'E'
			| posn<len-2 && s.[posn+1] == '-'
				= 	toRealWithExp s (posn+2) val 0 (-1) 0
			| posn<len-2 && s.[posn+1] == '+'
				= 	toRealWithExp s (posn+2) val 0 (+1) 0
			| posn<len-1
				= 	toRealWithExp s (posn+1) val 0 1 0 
				= 	0.0
			= 	0.0
		where
			c		=	s.[posn]
			n		=	toInt c  -  toInt '0' 
			digit	=	c>='0' && c<='9'

	toRealWithDot :: !{#Char} !Int !Real !Int -> Real
	toRealWithDot s posn val dn
298
		| posn == len
299 300 301 302 303 304
			| dn==0
				= 	val
				= 	val / (10.0 ^ toReal dn)
		| digit
				= 	toRealWithDot s (posn+1) (toReal n + 10.0*val) (dn+1)
		| c== 'e' || c== 'E'
305
			| posn<len-2 && s.[posn+1] == '-'
306
				= 	toRealWithExp s (posn+2) val dn (-1) 0
307
			| posn<len-2 && s.[posn+1] == '+'
308
				= 	toRealWithExp s (posn+2) val dn (+1) 0
309
			| posn<len-1
310
				= 	toRealWithExp s (posn+1) val dn 1 0 
311 312 313 314 315
				= 	0.0
			= 	0.0
		where
			c		=	s.[posn]
			n		=	toInt c  -  toInt '0' 
316 317 318 319 320 321 322
			digit	=	c>='0' && c<='9'

	toRealWithExp :: !{#Char} !Int !Real !Int !Int !Int -> Real
	toRealWithExp s posn val dn eneg eval
		| posn == len
			# e = eneg*eval-dn
			| e>=0
Camil Staps's avatar
Camil Staps committed
323
				=	val * 10.0 ^ toReal e
324 325 326 327 328 329 330 331 332 333
			| e>= -308
				=	val / 10.0 ^ toReal (~e)
				=	(val / 5.0 ^ toReal (~e)) / 2.0 ^ toReal (~e)
		| digit
			= 	toRealWithExp s (posn+1) val dn eneg (n + 10*eval)
			= 	0.0
		where
			c		=	s.[posn]
			n		=	toInt c  -  toInt '0' 
			digit	=	c>='0' && c<='9'
334 335 336 337 338 339

entier :: !Real -> Int
entier a
	= code inline {
			entierR
	}
340 341 342 343 344 345 346

Infinity :== 1E9999
NaN :== 1E9999+(-1E9999)

isNaN x :== if (x==x) False True
isInfinity x :== if (abs x==1E9999) True False
isFinite x :== if (x-x==0.0) True False