12.4. Controlling Precision of Display

This section deals with the control of the precision of display of floating point number on ProbeDisplays.

12.4.1. Global setting of precision

There are two types of global precision setting via :

To actually initialise these defaults: in the top level swarm, you should add the calls to the global probelibrary instance (which is actually created by the initSwarm call in main) during the - createBegin method (this sets the precision in the global instance, before any probes are checked out of the instance. If neither method is called on probeLibrary, then the precision defaults to six significant figures in both cases.


Example 12-3. Global setting precision in HeatbugObserverSwarm.m

+ createBegin: aZone
{
   HeatbugObserverSwarm * obj;
   id <ProbeMap> probeMap;

   [...]
   
   probeMap = [EmptyProbeMap createBegin: aZone];
   [probeMap setProbedClass: [self class]];
   probeMap = [probeMap createEnd];
        // set the display defaults
   [probeLibrary setDisplayPrecision: 3];
        // typically saved precision would be higher than displayed precision
     // for statistical and data analysis purposes
   [probeLibrary setSavedPrecision: 10]; 

   // Add in a bunch of variables, one per simulation parameters
   [probeMap addProbe: [probeLibrary getProbeForVariable: "displayFrequency"
   inClass: [self class]]];
   [...]

   // Now install our custom probeMap into the probeLibrary.
   [probeLibrary setProbeMap: probeMap For: [self class]];
   return obj;
}

12.4.2. Setting Precision for Individual Probes

The formatting for an individual probe can be set directly, using a sprintf-style formatting string. Typically, customProbeMaps are created in the +createBegin factory method for a Swarm or a SwarmObject. To set the formatting for a floating point probe, the method from VarProbe is used:

In the following example, it is desired that the number of significant figures for the floating point variable randomMoveProbability is three (3). Currently (Swarm 2.0.1) this is only works for VarProbes and not MessageProbes, as yet.


Example 12-4. Setting precision for individual probes in HeatbugModelSwarm.m:

+ createBegin: aZone
{
   HeatbugModelSwarm * obj;
   id <ProbeMap> probeMap;
   id floatProbe;

   [...]
        // the -setFloatFormat is applied to the probe which is 
     // "returned" from the call to probeLibrary
   floatProbe = [[probeLibrary getProbeForVariable: "randomMoveProbability"
   inClass: [self class]]
   setFloatFormat: ".3f"];
   
   // now we have the probe - put it back into the customMap
   [probeMap addProbe: floatProbe];

   [...]
   return obj;
}

Or, more compactly:

+ createBegin: aZone
{
  HeatbugModelSwarm *obj;
  id <ProbeMap> probeMap;

  [...]
  
  [probeMap addProbe: [[probeLibrary getProbeForVariable: "randomMoveProbability"
  inClass: [self class]]
  setFloatFormat: "%.3f"]];
  [...]
  return obj;
}