Is This Problem Obvious?

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

Is This Problem Obvious?

Edmund Chattoe-Brown
Dear All,

I have an attribute with values between 0 and 1 in increments of 0.1. When I initialise the agents and print what they are allocated, they all show the "tidy" values as you would expect. I then hatch their offspring and when I print the values they have inherited, these are also "tidy". Then, using ( ( ( random 3 ) - 1 ) / 10 ) - which I aspire should generate possible values +0.1, 0, -0.1. - I aim to "mutate" these inherited values. (This is a "gene" that determined what fraction of available "forage" each agent takes when it lands on a patch.) When I print out the values of this expression, they are also "tidy". However, when I add the "tidy" inherited value to the "tidy" mutation element I get some values which, instead of being "tidy", are things like 0.300000004 and 0.199999999. What does one have to do to ensure that two numbers that print as "tidy" actually remain "tidy" when added together? I can't tell if this is an issue with data types (the reason I use "random" is that it is supposed to generate only integers) or with what is displayed.

Thanks,

Edmund

--
  Edmund Chattoe-Brown
  [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is This Problem Obvious?

Ken Kahn-3
Perhaps you are looking for
http://ccl.northwestern.edu/netlogo/docs/dict/precision.html

See also the discussion of roundoff errors at
http://ccl.northwestern.edu/netlogo/docs/programming.html#math

-ken

On Tue, 10 Jul 2018 at 01:03, 'Edmund Chattoe-Brown'
[hidden email] [netlogo-users] <
[hidden email]> wrote:

>
>
> Dear All,
>
> I have an attribute with values between 0 and 1 in increments of 0.1. When
> I initialise the agents and print what they are allocated, they all show
> the "tidy" values as you would expect. I then hatch their offspring and
> when I print the values they have inherited, these are also "tidy". Then,
> using ( ( ( random 3 ) - 1 ) / 10 ) - which I aspire should generate
> possible values +0.1, 0, -0.1. - I aim to "mutate" these inherited values.
> (This is a "gene" that determined what fraction of available "forage" each
> agent takes when it lands on a patch.) When I print out the values of this
> expression, they are also "tidy". However, when I add the "tidy" inherited
> value to the "tidy" mutation element I get some values which, instead of
> being "tidy", are things like 0.300000004 and 0.199999999. What does one
> have to do to ensure that two numbers that print as "tidy" actually remain
> "tidy" when added together? I can't tell if this is an issue with data
> types (the reason I use "random" is that it is supposed to generate only
> integers) or with what is displayed.
>
> Thanks,
>
> Edmund
>
> --
> Edmund Chattoe-Brown
> [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Is This Problem Obvious?

Nicolas Payette-2
In reply to this post by Edmund Chattoe-Brown
Hi Edmund,

However, when I add the “tidy” inherited value to the “tidy” mutation
element I get some values which, instead of being “tidy”, are things like
0.300000004 and 0.199999999.

This is one of the joys of floating point arithmetic
<https://en.wikipedia.org/wiki/Floating-point_arithmetic>:

observer> show 0.1 + 0.2
observer: 0.30000000000000004

Any language using double precision floating point numbers would give you
this result, not just NetLogo.

What does one have to do to ensure that two numbers that print as “tidy”
actually remain “tidy” when added together?

Use the precision
<http://ccl.northwestern.edu/netlogo/docs/dictionary.html#precision>
primitive, e.g.:

observer> show precision (0.1 + 0.2) 1
observer: 0.3

I can’t tell if this is an issue with data types (the reason I use “random”
is that it is supposed to generate only integers) or with what is displayed..

There is no such thing as an integer data type in NetLogo. It’s doubles all
the way down
<https://ccl.northwestern.edu/netlogo/docs/programming.html#math>. It’s
usually not a problem, but it does mean that you can run into issues like
this from time to time. Using precision
<http://ccl.northwestern.edu/netlogo/docs/dictionary.html#precision> in the
right places is usually enough to get around those.

Cheers,


Nicolas


On Tue, Jul 10, 2018 at 1:03 AM 'Edmund Chattoe-Brown'
[hidden email] [netlogo-users] <
[hidden email]> wrote:

>
>
> Dear All,
>
> I have an attribute with values between 0 and 1 in increments of 0.1. When
> I initialise the agents and print what they are allocated, they all show
> the "tidy" values as you would expect. I then hatch their offspring and
> when I print the values they have inherited, these are also "tidy". Then,
> using ( ( ( random 3 ) - 1 ) / 10 ) - which I aspire should generate
> possible values +0.1, 0, -0.1. - I aim to "mutate" these inherited values..
> (This is a "gene" that determined what fraction of available "forage" each
> agent takes when it lands on a patch.) When I print out the values of this
> expression, they are also "tidy". However, when I add the "tidy" inherited
> value to the "tidy" mutation element I get some values which, instead of
> being "tidy", are things like 0.300000004 and 0.199999999. What does one
> have to do to ensure that two numbers that print as "tidy" actually remain
> "tidy" when added together? I can't tell if this is an issue with data
> types (the reason I use "random" is that it is supposed to generate only
> integers) or with what is displayed.
>
> Thanks,
>
> Edmund
>
> --
> Edmund Chattoe-Brown
> [hidden email]
>
>