Transform - Numbers: Copy

The Copy template appears in the template list when a numeric field of type float32, float64, int8, int16, int32, int64uint8, uint16, uint32, or uint6, has been picked in the Transform pane.

 

Copy

Copy into the specified numeric Result destination the values in the source field,  using the specified numeric type.

 

Launch the template by choosing a numeric field and then double-clicking the Copy template.   When the template launches we can specify options.

 

 

Copy the source field into the specified Result destination.

 

We start with a table with a single Numbers field of type float64.   We have used the Layers pane to hide the mfd_id field, for a simpler illustration.

 

 

With the focus on the table window, in the Transform pane we choose the Numbers field and then the Copy template.

 

 

In the Copy template, for the Result destination, we choose New Field and then enter Numbers copied as the name of the new field to add to the table.  We could use whatever name we want, but it is wise to use names that remind us of what they are supposed to be.   We leave the Result type at the default float64.

 

If we preferred, we could have chosen some other existing numeric field in the table as the Result destination, and thus copied the values from Numbers into that field.

 

Press Transform.  

 

 

The template creates a new float64 type field called Numbers copied and populates it with copies of numbers in the Numbers field.

Converting Data Types

The Copy template is useful for converting numeric data types.   

 

For example, suppose we would like to convert the values from the Numbers field from a float64 data type into an int32 data type.

 

 

The tooltip for column headers shows the data type of the field.

 

With the focus on the table window, in the Transform pane we choose the Numbers field and then the Copy template.

 

 

In the Copy template, for the Result destination, we choose New Field and then enter Integer numbers as the name of the new field to add to the table.  

 

In the pull down menu for Result type we choose int32.

 

Press Transform.  

 

 

The template creates a new int32 type field called Integer numbers and populates it with copies of numbers in the Numbers field, automatically casting (converting data type) on the fly from float64 to int32.

 

While the automatic conversion is convenient, we will want to manually intervene in cases where conversion between data types involves user choice for how conversions are done.  

 

 

Consider a table full of numeric values in float64 data type that include fractional parts.   

 

 

We can use Copy to create an Integers field that is type int32 and which is populated with converted values from the Fractions field.

 

 

However, the result, seen above, may not be what we want, because the straight conversion of a float64 to an int32 simply takes the integer part of the number, discarding the fractional part.  That is the same as rounding to zero (trunc), a truncation of the floating point number to an integer number.  It is not the same as rounding up or down as usually expected for rounding.

 

We can that effect, rounding to the nearest value, using the Round template.   

 

With the focus on the table window, in the Transform pane we choose the Fractions field and then the Round template.

 

 

In the Round template, for the Round option we choose to nearest (round), with Round to set to 1, that is, rounding to the first digit to the left of the decimal point, thus rounding to whole integers.

 

for the Result destination, we choose New Field and then enter Rounded integers as the name of the new field to add to the table.   In the pull down menu for Result type we choose int32.

 

We want to create a new field to show how in one step we can do both a data conversion and a rounding operation, and also so we have a new field in the table that we can visually compare to the Integers field we created and populated in the prior example.  

 

However, if we preferred, for the Result destination we could have chosen from the pull down menu the Integers field created in the previous example which we created as an int32 field.

 

Press Transform.  

 

 

The template creates a new int32 type field called Rounded integers and populates it with copies of numbers in the Fractions field that have been rounded up or down to the nearest whole integer value, automatically casting (converting data type) on the fly from float64 to int32.

 

We can compare the difference between the Rounded integers results and the Integers results.  The straight truncation of floating point numbers to integers used to populate the Integers field is the same as rounding to zero (trunc).   For example, the 4464.649... value in the second record is rounded down to 4464, but using to nearest (round) to populate the Rounded integers field rounds that same number up to 4465.

 

The point of the above is that different data types are used for a reason, to capture information about data that other data types might not be able to convey, so to convert between different data types we may have choices as to how we want that conversion to occur.  Manifold provides ways to let us make conversions as we prefer.

Using Edit - Query to Round and Transform within Copy

Simple modifications in queries created by the Edit - Query button are a great way to accomplish simple tasks like rounding, copying data from one data type into another, and creating a new field all in one step.

 

We start with a table that has a single float64 field called Fractions.  We would like to create an equivalent field using the int64 data type that is rounded up or down to the nearest integer, and not truncated.

 

 

With the focus on the table window, in the Transform pane we choose the Fractions field and then the Copy template.

 

 

For the Result destination we choose New Field and enter Int64 copy as the name of the new field.   For the Result type we choose int64.

 

Press Edit Query.

 

A new Command Window opens, populated with the query seen above.     The operative portion of the query is:

 

-- prepare begin

 

ALTER TABLE [Numbers] (

  ADD [Int64 copy] INT64

);

-- prepare end

 

UPDATE [Numbers] SET

  [Int64 copy] = [Fractions];

 

The first part prepares the table by adding a new field of type int64.   The second part does the copy, relying on an implicit conversion, which will end up truncating the floating point values to the integer part.  

 

If we want the fractional numbers to be rounded up or down to the nearest integer, we simply edit the query to use the Round function:

 

-- prepare begin

 

ALTER TABLE [Numbers] (

  ADD [Int64 copy] INT64

);

-- prepare end

 

UPDATE [Numbers] SET

  [Int64 copy] = Round([Fractions]);

 

The Command Window now shows:

 

 

Press the ! run button in the main toolbar to run the query, and the Numbers table is updated:

 

 

A new Int64 copy field appears in the table, of data type int64, with numbers copied from the Fractions field but rounded up or down to the nearest integer.

 

 

See Also

Transform Pane

 

Transform Reference

 

Transform - Expression

 

Transform - Numeric Vectors

 

Transform - Numbers

 

Transform - Numbers: Arithmetic

 

Transform - Numbers: Bit Logic

 

Transform - Numbers: Expression

 

Transform - Numbers: Hyperbolic

 

Transform - Numbers: Limit

 

Transform - Numbers: Random

 

Transform - Numbers: Round

 

Transform - Numbers: Special

 

Transform - Numbers: Trigonometric