13.4.2.1 using units of measure

Source: Internet
Author: User

13.4.2.1 using units of measure

In F #, it is easy to use units of measure, which is why we look at this chapter simply for a brief explanation. Claim metering uses the Type keyword, plus proprietary attributes. Strictly speaking, metering is not a type, but we can use it as part of another type. We first define two simple meters, which represent kilometers and hours:

[<Measure>]type km[<Measure>]type h

It can be found that the type described using the Measure attribute is metered. This is a proprietary feature that the F # compiler can understand. We don't have to define units ourselves, we can use the standard set in the FSharp.PowerPack.dll library, but for now, we'll declare ourselves. With the Miles and H, you can create a value that represents the kilometer or hour. Listing 13.15 shows the values for creating a band unit, and writes a function that participates in the calculation.

Listing 13.15 calculations using units of measure (F #)

> LetLength =9.0<km>;; [1]ValLength:float<km> =9.0> length * length; [2]Valit:float<km^2> =81.0> LetDistanceintwohours (Speed:float<km/h>) = [3] Speed *2.0ValDistanceintwohours:float<km/h>float<km> [4]> Distanceintwohours (30.0<km/h>);Valit:float<km> =60.0

When describing units of numeric constants, the values are followed by units enclosed in angle brackets [1]. We first define a value expressed in length, in kilometers. F # can automatically infer the unit of the result if the calculation uses a value with a unit, so it can be found that the distance is multiplied by two times and the area is measured in square kilometers [2]. When describing a unit, you can use a regular representation, that is, to represent a exponentiation with a ^, divide with/represent, and multiply as a parallel unit.
The next example shows that the parameters of a function can include unit information. The parameters of our example function are speed, returning two hours of walking distance [3]. We specify that the parameters are measured in kilometers per hour, so the type annotations that contain the units are added, and the units are placed in angle brackets in the same way as the value of the specified type parameter, such as the list type. The F # compiler is able to infer the returned type [4], just as it does with a normal type. Often, when we read the code, we can provide clues to our understanding of function functions, and this is also an important check for writing functions, which avoids making a low-level error: If we try to calculate the distance, but the unit of the last return type is time, then it must be wrong.
In the World Bank data, we will use km^2 to represent the total area of the country's units. So far, everything goes well, but the second indicator we want is provided as a percentage, so how do you specify a percentage unit? Although units of measure are primarily used to represent physical quantities, they can also be used to represent percentages:

[<Measure>]type percentlet33.0<percent>

This code creates a unit that represents a percentage, and then defines the constant Coef, which is 33% of the value. Strictly speaking, the value in percent does not have a unit, because, this is a factor, it is useful to define it as a unit. In order to demonstrate, we calculate 50 km distance of 33%. Since the coef represents a coefficient, you can simply multiply the two values:

> 50.0<km> * coef;;val it : float<km percent> = 1650.0

It's obvious that this is wrong. We expect the results to be measured in kilometers, however, from the deduced type can be found, the result is multiplied by the new unit percent. In addition, the code that runs from the interaction can also see that the values are too large, and the importance of the unit of measure is that we can find the error during the type check without having to wait until the program is actually running. So, where's the problem? The percent value represents the coefficient multiplied by 100; to write the calculation correctly, you need to divide the value by the percent:

> 50.0<km> * coef / 100.0<percent>;;val it : float<km> = 16.5

Can be found, now much better. The result is divided by the percent so that there is no percent unit in the result. F # Automation Simple unit, knowing km percent/percent is equal to KM. This example demonstrates the important reason for using units of measure: Just like other types, there is a way to help catch a large number of errors as early as possible.

Attention

There are many other important functions in the unit of measurement that we did not refer to in this introduction. For example, you can define a derived unit, such as N (for Force, in Newton), which is actually kg m/s^2; in a function or type, it is possible to use units for generic type parameters. For more information about units of measure, see the F # online documentation, and the architect Andrew Kennedy blog about the feature (Http://blogs.msdn.com/andrewkennedy).

Now, let's go back to the main example and convert the downloaded data into a typed form with units. We will use atomic units percent to represent area forest cover, with unit km^2 to represent area.

13.4.2.1 using units of measure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.