This section deals with the control of the precision of display of floating point number on ProbeDisplays.
There are two types of global precision setting via :
-setDisplayPrecision: (int) nSigFigsSaved Sets the number of significant figures for floating point (and double-floating) numbers diplayed on a GUI widget. Currently this is only implemented for VarProbes. The display uses the %*g sprintf-style formatting, which can vary slightly from implementation to implementation. If you set the number of significant figures to 3, then a float of value of 0.6344346 is displayed as 0.634 on the GUI widget.Note that this in no way affects the underlying stored value of the floating point number.
-setSavedPrecision: (int) nSigFigsSaved Sets the global default for the saving of floats through ObjectSaver. All objects with floats and doubles as instance variables are saved with the precision specified by this method. This is independent of the displayed precision of the same instance variable on a GUI widget.
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; } |
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:
-setFormatFloat: (const char *)floatFormat is applied to the instance of the VarProbe "checked-out" of the global probeLibrary instance. The sprintf-formatting string can "over-ride" the "%g" format set by the global precision (as above) (Typically "%g" chooses between the "%f" and "%e", depending on the size of the exponent - which is implementation-dependent - this method allows you to explicitly set the type of display).
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; } |