(little) Bugreport in round()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

(little) Bugreport in round()

Peter Falke

Dear developers,
 
by chance I saw a little bug with the 'round()' function.
I made this test program to chase it down (see also attachment):
 
// TestRound v1_0

// GPLv2

// (c) 2012 TakeItAndRun


echo();

echo("Test of the 'round(var)' funktion in OpenSCAD :");

echo();


echo("round(1.2): ",round(1.2));

echo("round(0.2): ",round(0.2));

echo("round(0.0): ",round(0));

echo("round(-0.2):",round(-0.2));

echo("round(-1.2):",round(-1.2));


echo();

a=round(0);

b=0;

echo("a=round(0),b=0:",a,b);

echo();

echo("if (a==b) gives:");

if (a==b) {echo("if: 0 equals -0");}else{echo("if: 0 UN- equals -0");}

// at least -0 is equal to 0


echo();


echo("sign(-0,sign(0):",sign(a),sign(b));

// and sign() of 0 and -0 is always 0

echo("sign(-1),sign(1):",sign(-1),sign(1));


echo();


echo("'sign(var)' works 'correct' for '0' and '-0'");


echo();


echo("echo a,b:",a,b);


echo();


echo("Result: in the range var=[0,-.999999] the 'round(var)' funktion returs '-0',");

echo(" but it treats it internally as '0'");


echo();

 
 
It produses the following output:
 
Parsing design (AST generation)...

Compiling design (CSG Tree generation)...

ECHO:

ECHO: "Test of the 'round(var)' funktion in OpenSCAD :"

ECHO:

ECHO: "round(1.2): ", 1

ECHO: "round(0.2): ", 0

ECHO: "round(0.0): ", -0

ECHO: "round(-0.2):", -0

ECHO: "round(-1.2):", -1

ECHO:

ECHO: "a=round(0),b=0:", -0, 0

ECHO:

ECHO: "if (a==b) gives:"

ECHO: "if: 0 equals -0"

ECHO:

ECHO: "sign(-0,sign(0):", 0, 0

ECHO: "sign(-1),sign(1):", -1, 1

ECHO:

ECHO: "'sign(var)' works 'correct' for '0' and '-0'"

ECHO:

ECHO: "echo a,b:", -0, 0

ECHO:

ECHO: "Result: in the range var=[0,-.999999] the 'round(var)' funktion returs '-0',"

ECHO: " but it treats it internally as '0'"

ECHO:

Compilation finished.

Compiling design (CSG Products generation)...

ERROR: CSG generation failed! (no top level object found)

PolySets in cache: 1

Polygons in cache: 6

CGAL Polyhedrons in cache: 0

Vertices in cache: 0

 

This does not really lead to any program errors, but It looked a hell of a lot confusing when writing and debugging my program.
 
Greetings,
 
TakeItAndRun
 


--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!


TestRound v1_0.scad (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: (little) Bugreport in round()

chrysn
hello peter,

On Fri, May 25, 2012 at 06:37:30PM +0200, Peter Falke wrote:
> by chance I saw a little bug with the 'round()' function.
> I made this test program to chase it down (see also attachment):

i've run your program, but i don't see what you're trying to get at.
negative zero is a typical phenomenon of floating point numbers -- it
exists, but it doesn't hurt, because it acts exactly like 0 for almost
all purposes.

could you explain what bothers you about the output?

a program like openscad could choose to hide that difference by
displaying "-0" as 0, but that would make the subtle cases where 0 is
different from -0 even more difficult to track down, and whoever works
with numbers on a pc needs to be aware of the constraints of floats
anyway.

btw, please refrain from copy/pasting large code blocks into emails,
especially when you write html mails.

regards
chrysn

--
To use raw power is to make yourself infinitely vulnerable to greater powers.
  -- Bene Gesserit axiom

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: (little) Bugreport in round()

Peter Falke
Your right it does not seem to affect the working of OpenSCAD at all.

I was not aware that this is a known problem with float numbers.

I guess it bothered me because I'm from back when you had only 8/16-bit numbers and a zero- and neg-bit was implemented in the hardware of the processor.

Thanks for your reply

Pete