Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 6-Jan-2019 02:27:04

ochoapigo
Member
Registered: 27-Apr-2018
Posts: 16

Compare numbers negatives in scol?

Hi, I need help, how can compare numbers negatives in lenguaje scol?

For example:

let  atof "$9" -> x in
let " " -> temp_x in

if x =. -255.0 then
(
set temp_x = ftoa(-1.0)
)
else(
set temp_x = ftoa(0.0)
)

This, show error in console.

Thanks so much

Offline

#2 6-Jan-2019 14:06:17

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,023
Website

Re: Compare numbers negatives in scol?

Hi !

Several errors in your code.

You should never compare floats in this way, in Scol nor any other language.

To compare two integers, it's ok :

if integer1 == integer2 then
  ...

To compare two floats, you compare the difference with an epsilon whose accuracy you arbitrarily choose.

if (float1 -. float2) <. epsilon then
  _fooS "they are equals"
else
  _fooS "They are not equals";

You should use the float Scol library : http://redmine.scolring.org/projects/li … float.html (doc)

Your code could be this in h/a.pkg :

fun main()=
	_showconsole;
	let  atof "$9" -> x in
	let " " -> temp_x in
	if std_fCmp x (-.255.0) then
		set temp_x = ftoa (-.1.0)
	else
		set temp_x = ftoa 0.0;;

Called by a such script :

_load "lib/std/float.pkg"
_load "h/a.pkg"
main

I use std_fCmp function. By default, epsilon is 0.000001.

You write :

if x =. -255.0 then

Also, = sets a value to a variable, so if x = 255.0 then sets 255.0 to x and the expression tests the return value of set, a side effect. This is an equivalent of if set x = 255.0 then ...
if x =. 255.0 then, with a point, create a syntax error : this is no sense in Scol. It's contrary to its grammar.
In a test, you must double the equal sign but with the float numbers, this is wrong (see above).

Next, -255.0 is wrong. Indeed, - (without point) expects an integer.
-. (with point) expects an float, thus (-.255.0) is correct.

If you want compare an integer and a flot, you must convert the integer to float (or the other way around but your can lost the precision)

Then, your variable named x is stange :

let  atof "$9" -> x in 

will always give 0.000000. I guess you wish to get the value of $9, maybe a parameter into a string ?

Offline

Board footer

Powered by FluxBB