Learning LabVIEW (10) - About Matlab's eps function (13)

Publisher:HarmoniousCharmLatest update time:2015-08-19 Source: eefocusKeywords:LabVIEW Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    I have written more than a dozen short articles about the implementation principle of Matlab's eps function. The version closest to the official one can be found in "About Matlab's eps function (Ten) - C code generated by MATLAB Coder" . At that time, MATLAB Coder was used to directly generate a C language version of eps, which should be the official implementation method.
    Why are you so concerned about the eps function? Because eps is very important for numerical analysis. It shows a basic property of IEEE754 floating-point numbers: relative floating-point precision. However, despite the importance of eps, few people can clearly explain the eps calculation method. In modern society, everyone needs to know how to program. However, if you have only received ordinary introductory programming training and have not spent many hours studying numerical analysis like those who graduated from computer majors, you will tend to equate the concept of floating-point numbers in computers with the concept of real numbers in mathematics. But the fact is: there is a huge difference between floating-point numbers and real numbers in mathematical concepts. For example, real numbers in the concept can take continuous values. Floating-point numbers based on the 754 standard, like integers, have discrete values! EPS is a tool to measure the distance between one floating point value and the next floating point value. Unlike integers, the interval between floating point numbers is not fixed, but changes with the value of the floating point number; so EPS is not a constant but a function.
  • In "About Matlab's eps function" , we discussed the nature of eps and used Matlab's typecast function (used to implement reinterpret cast) to implement the calculation of eps;
  • In "About Matlab's eps function (continued)" , "About Matlab's eps function (continued again)" , "About Matlab's eps function (six)" and "About Matlab's eps function (seven) — the continuation of "continued again" , we implemented the eps calculation using bit-field structures. This version is much more readable than the original version implemented using typecast.
  • In "About Matlab's EPS Function (Continued)" , we discussed the method of implementing EPS using pure mathematical operations instead of bit operations based on a post on MathWorks.
  • In "About Matlab's eps function (V)" and "Article 4 in 1: About Matlab's eps function (VIII) & Read Matlab7.7's rank function & Read Matlab7.7's orth" , several practical applications of the eps function are discussed;
  • In "About Matlab's eps function (Part 9) - Java also has an "eps" function" , we showed the usage of ulp, a method similar to eps in Java, and discussed the differences between ulp and eps.
  • In "About Matlab's eps function (10) - C code generated by MATLAB Coder" , we took a peek at the official eps implementation method through MATLAB Coder;
  • In "GPU Powered Matlab (Part 3) - About Matlab's eps function (Part 11)" and "GPU Powered Matlab (Part 3.1) - About Matlab's eps function (Part 12)" , we tried to move eps to the CUDA GPU for execution.
    What we are going to do today is to implement eps operation in LabVIEW using pure G language. Actually, I don't need to use eps in LabVIEW now, but I just want to understand how powerful LabVIEW's bit operation ability is by implementing eps.
    First, LabVIEW provides a node named "Computer ɛ" in "Programming->Numerical". This node is a constant node (not a function), and its value is equal to eps(1) in MATLAB: The value output by the numerical display control "Numerical 3" in the figure above is 0, which means that the LabVIEW node "Computer ɛ" is equal to eps(1) in Matlab. In fact, since MATLAB functions support variable number of parameters and brackets can be omitted when calling functions, eps(1) can also be written in the form of eps, which is why many people think that eps in Matlab is a constant rather than a function. If you are diligent, you can see more information about eps by typing "doc eps" in the Command Window of Matlab.  Since the "Computer ɛ" node is not a function, we need to add additional programs to implement the function of eps. In fact, C++ also provides the value of eps(1). The implementation method in "About Matlab's eps function (VI)" is to use bit operations to obtain the exponential part E of the given floating-point number R, and then use the expression eps(1) * 2 ^ E to get the value of eps(R). We can also use this method here.  In order to extract the exponential part of the floating-point number, we first test the bit operation ability of LabVIEW. If you want to perform bit operations on floating-point numbers, you need the language to provide a reinterpret cast mechanism. In Matlab, it is typecast, and LabVIEW also provides typecast. According to the documentation, LabVIEW's typecast node can implement: *(type *) &R conversion, which is exactly what we need.  Floating-point numbers are essentially no different from integers. They are just a collection of several bytes in memory, and the values ​​of the bits in them contain information. The only difference from integers is the way to interpret these bits. However, programmers who have not learned low-level languages ​​are taken care of by high-level language compilers/interpreters, and the details of floating-point storage and calculation are completely covered by the language mechanism. This is a good thing, because people don't have to worry about too many details when programming. This is also a bad thing, because people don't have to understand the underlying mechanism of floating-point numbers. Over time, people have formed a wrong impression: since the language does not allow me to perform bit operations on floating-point numbers, floating-point numbers probably cannot be operated on. When I was in college, there was a project team next door. Because they didn't know how to split floating-point numbers into byte arrays in C language to put them into the serial port buffer, they almost changed the hardware circuit.  The usage of LabVIEW's typecast is almost the same as Matlab's typecast function. Let's review Matlab's typecast function first.  First, set the output format to hexadecimal: >> format hex Take a double-precision floating-point number 15 and convert it into a byte array: >> bytes = typecast(15, 'uint8') bytes =  00   00   00   00 00   00   2e   40   We know that floating-point numbers are composed of three parts: sign, exponent, and mantissa. Here we try to change the highest bit of the last byte of the byte array to 1. The corresponding floating point number is the sign bit becomes 1, which will become a negative number: >> bytes(end) = hex2dec('c0'); >> format; dbl = typecast(bytes, 'double') dbl =  -15 Then try to operate on the exponent part of the floating point number. Double-precision floating point numbers have 8 bytes, 64 bits. The sign bit is 1 bit, the exponent is 11 bits, and the mantissa is 52 bits. If you want to add 1 to the exponent (equivalent to multiplying the floating point number by 2), just perform the following operation: >> typecast(typecast(15, 'uint64') bitshift(uint64(1), 52), 'double') ans =  30 First, reinterpret the double-precision floating point number 15 into unsigned int64, then add 1 and shift it left 52 bits, and then reinterpret it into double. It's that simple. [page]  The above two examples are implemented in LabVIEW using G language as follows: A very important point is that the blue numeric constants above need to be manually specified for their specific integer type. Right-click in the blue box of the constant and select "Representation->UINT64" in the pop-up menu to explicitly specify the specific integer type of the data, otherwise the operation will go wrong and the expected result cannot be obtained.  Through the above experiments, we finally realized that LabVIEW has bit operation capabilities similar to C. The next step is to use typecast and bitwise AND, left shift and right shift to extract the exponent of the floating-point number? In fact, it is not necessary to be so troublesome. LabVIEW provides a special node for extracting the exponent and mantissa of floating-point numbers. Try to use the "Mantissa and Exponent" node to get the exponent of the floating-point number 15: This is exactly the value we need. 15 is between 8 and 16. 8 is 2 to the power of 3 and 16 is 2 to the power of 4, so the exponent part of 15 is 3. Now multiply "Computer ɛ" by 2 cubed (using the "Scale by Power of 2" node) to get the value of eps(15): We compare the calculated value with Matlab's eps(15). The "Value 5" control shows the calculated result of 1.77636E-15; the "Value 6" control shows the difference between the result calculated by LabVIEW and the result calculated by Matlab, and the output is 0, indicating that it is consistent with Matlab. Using such a simple G language block diagram, we have realized the calculation of eps.
Learning LabVIEW (10) - About Matlab's eps function (13)


   


   


   
   
   






         







  






   


   
Learning LabVIEW (10) - About Matlab's eps function (13)


Learning LabVIEW (10) - About Matlab's eps function (13)

   
Learning LabVIEW (10) - About Matlab's eps function (13)


Learning LabVIEW (10) - About Matlab's eps function (13)

Keywords:LabVIEW Reference address:Learning LabVIEW (10) - About Matlab's eps function (13)

Previous article:Learning LabVIEW (XI) - UTF8 string decoding
Next article:Learning LabVIEW (Part 9) - Simple Simulation of Matched Filtering Part 2

Recommended ReadingLatest update time:2024-11-16 14:53

Using ATmega16 to make a simple virtual oscilloscope
This paper designs a simple virtual oscilloscope using the LabVIEW virtual experiment software platform, which uses the ATmega16 microcontroller for data acquisition and transmits data to the PC via RS232 serial communication. Users can set and adjust data acquisition parameters and store waveform data on the developm
[Test Measurement]
Using ATmega16 to make a simple virtual oscilloscope
Labview: Conditional Structure and Event Structure
The conditional structure and the event structure are generally similar in that they both need to handle a branch. However, they also have differences, as shown in the following: 1. The conditional structure must have a branch to be executed, otherwise an error will occur. The time structure can wait for the occurrenc
[Test Measurement]
Using MATLAB to model and simulate smart cars
Brief introduction to the structure of two intelligent cars The two most common smart cars on the market are both wheeled. The most popular ones sold on Taobao are the various cars that you use to cope with course design and graduation projects during your student days. There are two types. 1. Rear-w
[Embedded]
Using MATLAB to model and simulate smart cars
Labview Problem Description
If you want to get the time two days later by getting the time control, just add the number 172800. The default is to add that many seconds. If you want to call two identical subVIs in the same program, they cannot run in parallel if the VI properties are not set to reentrant, because the local variable data of each
[Test Measurement]
Features of LabVIEW
LabVIEW combines an easy-to-use graphical development environment with the flexible and powerful G programming language, providing a very intuitive programming environment; it has additional development tools designed specifically for large-scale application development, collective development, and application configur
[Test Measurement]
PWM Circuit Function Automatic Test System Based on LabVIEW
Automatic test system (ATS) is a general term for a type of system that can automatically complete measurement, data processing, and display (output) test results. In different technical fields, the test content, requirements, conditions, and automatic test systems are different, but they all use computers to replace h
[Test Measurement]
PWM Circuit Function Automatic Test System Based on LabVIEW
LabVIEW Series - Timed Loop Patterns
 
[Test Measurement]
Using LabVIEW, PXI, DAQ, and DIAdem to Build a Sonar Dome Monitoring System
challenge: Verification of the design and integration of different components inside the sonar dome of the Italian Navy's multi-purpose frigates (FREMM). Solution: Develop a sonar dome monitoring system (SDMS) based on LabVIEW and NI PXI data acquisition (DAQ) hardware to collect and process a
[Test Measurement]
Using LabVIEW, PXI, DAQ, and DIAdem to Build a Sonar Dome Monitoring System
Latest Test Measurement Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号