You can use a Float attribute in MDriven Designer to model an approximate fractional value when small floating-point rounding differences are acceptable.
What is Float?
Float is a floating-point numeric data type. It stores a number that can have an integer part and a fractional part, such as 3.5 or -0.25.
Float values use binary floating-point representation. This makes Float suitable for values where approximate numeric results are acceptable, but it can introduce small rounding differences for some decimal values.
For example, a Float attribute can represent a measurement used in a calculation:
Length = 12.5
Factor = 1.2
Result = Length * Factor
Choose the right numeric type
Choose the attribute type based on whether the value must remain exact.
| Type | Use it when | Example |
|---|---|---|
| Float | The value is an approximate fractional measurement or calculation result. | A calculated measurement of 12.5.
|
| Double | You need a floating-point value with higher precision and range than Float. | A calculation that requires more significant digits. |
| Decimal | Decimal places must be stored and calculated precisely. | A price of 19.95, a tax amount, or a monetary total.
|
| Integer | The value must be a whole number. | A quantity of 12 items.
|
Use Decimal for money and other values where exact decimal arithmetic is required. Do not use Float when a small rounding difference would produce an incorrect business result.
Use Float in OCL
In OCL, numeric expressions can perform standard arithmetic such as addition, subtraction, multiplication, and division.
For example, this expression calculates an adjusted measurement:
self.Length * 1.2
When an OCL expression is evaluated in the context of a simple value such as a numeric type, MDriven exposes the operations available for that simple type. See Documentation:Examples on collection operators for details about simple-type operations in the OCL editor.
Rounding and equality
A Float is not an exact representation of every decimal value. A calculation can therefore produce a value that is very close to the expected result without being identical to it.
Avoid relying on exact equality when the values come from floating-point calculations. Instead, compare whether the difference is within an acceptable tolerance for the measurement or calculation.
For example, if two calculated lengths are expected to be equal within 0.001, test whether their difference is smaller than that tolerance rather than requiring exact equality.
Convert between numeric types
MDriven supports conversions between numeric types in OCL and EAL. Converting a value can change its precision, and converting to an integer removes the fractional part.
Use toDecimal when an expression must produce a Decimal value:
self.SomeDecimal := self.SomeFloat.toDecimal
For example, if an assignment fails because one side is Double and the other is Decimal, convert the source value to the type required by the target:
self.PaymentMenuRequest.VatPercent := vTypAvBiljett.BiljettPrisMoms.toDouble
Review Documentation:Number conversions before converting values between Float, Double, Decimal, and Integer. For the toDecimal operator, see Documentation:OCLOperators toDecimal.
Units and presentation
You can use Float attributes with MDriven's metric/imperial presentation support. Store the value in metric units and configure the relevant attribute tagged value to display and edit the value in imperial units when the user setting requires it.
For example, a Float length value stored in millimetres can use the MillimeterInch MetricImperial tagged value. See Documentation:MetricImperial for the supported conversions, user settings, and display-decimal behavior.
See also
- Documentation:Double
- Documentation:Decimal
- Documentation:Number conversions
- Documentation:OCLOperators toDecimal
- Documentation:MetricImperial
[[Category:Attributes and data types [Beginner]]
