jess-users  

Re: JESS: Rounding a float number

ANA TANASESCU
Sun, 29 Nov 2009 12:49:03 -0800

Thanks Ernest,

I will use the format function and I will use the float value for the slot 
because I dont need a very accurate precision.

Ana

--- On Sun, 11/29/09, Ernest Friedman-Hill <ejfr...@sandia.gov> wrote:

From: Ernest Friedman-Hill <ejfr...@sandia.gov>
Subject: Re: JESS: Rounding a float number
To: "jess-users" <jess-users@sandia.gov>
Date: Sunday, November 29, 2009, 7:45 PM

Floating-point data types, like the Java ones used in Jess, are actually 
incapable of representing many decimal values exactly. Even a number as common 
as (for example) 0.11 can't be represented exactly, which is why in either Java 
or Jess, you'll get a result like this:

Jess> (/ 1.1 10)
0.11000000000000001

So what programmers typically do is carry around the full precision, then 
format the numbers for printing with the right number of decimal places.

If you really do need to carry precise values around -- for example, if your 
decimal values represent money -- then you can't use "float" (or RU.FLOAT). You 
have to use something more accurate. This is as true in Jess as it is in Java, 
C, C++, or any other language that uses IEEE floating point types or similar.

Two popular choices for "something more accurate" in Java are 1) for money, use 
integers to represent the number of whole cents, rather than decimal dollars, 
or 2) use the java.math.BigDecimal class, which gives exact values, but is 
computationally somewhat less convenient.



On Nov 29, 2009, at 10:01 AM, ANA TANASESCU wrote:

> Hello everyone,
> 
> I have a fact that contains a float slot. The value for this slot is obtain 
> by calculations.
> Is there in Jess a function that rounds the float number to a specified 
> number of decimal. I have searched into the Jess functions library and I 
> found the round function, but it returns an integer number not a float number.
> I  have, also, found the function format that uses the format specificators 
> from C++ like this:
> (bind ?a 9)
> (bind ?b (/ ?a 13))
> (printout t ?b )
> (format t "%2.2f" ?b)
> 
> Instead of this I want that the value of the variable ?b to be converted to a 
> number.
> 
> Best regards,
> Ana
> 
> 
> 

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, Livermore, CA 94550
http://www.jessrules.com







--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users y...@address.com'
in the BODY of a message to majord...@sandia.gov, NOT to the list
(use your own address!) List problems? Notify owner-jess-us...@sandia.gov.
--------------------------------------------------------------------