Recent Post

MATLAB : Fourier transform

Image
By Nuno Nogueira, CC BY-SA 2.5, https://commons.wikimedia.org/w/index.php?curid=3912826 MATLAB has been  crucial programming language across worldwide that let us express matrix and array mathematics directly. In signal processing and communication it plays vital role in analysing and exploring time series data in time domain or frequency domain. For analysing non periodic signals in frequency domain we use Fourier transform instead of Fourier series ,which is used to express periodic signals in terms of infinite sum of sinusoidal terms .   Fourier transform can exist for :      1. Energy signals      2. Power signals       3. Impulse signals  That means absolutely integrable signals can have their Fourier transform only.  Formula for Fourier transform;   Then its come to use Fourier transform in MATLAB. So here is the code for Fourier transform of sinusoidal wave , for any other signal we ...

Setting decimal precision in c/c++

Setting decimal precision in c/c++

In C++ and C precision and accuracy are problems which arise when using floating-point value. Now the question arises what is Precision and Accuracy? Precision is the total number of digits in any number. For example, 2.31 has 3 precision value, 0.4 has one precision. Accuracy refers to the degree of exactness or correctness of a number.

we well know about pi. It is an irrational number. The value of Pi varies according to the accuracy of the calculations.NASA uses around 15 digits of pi to send rockets into space. But for small calculations that not need more precision and accuracy, we have to round off the value of pi up to certain digits. Generally, we take the value of pi equal to 3.14 for small calculations. Because the third decimal place, 1, is less than 5, we can simply drop the third and subsequent places when rounding.

Now the question is how to precise our floating-point value in C/C++? There are many methods and functions available to write precise decimal values in C and C++. Here I am discussing commonly used methods.

In C++ we use setprecision() function.It takes an argument. The argument decides up to how many digits you want to round off your value.when it used along with ‘fixed’, provides precision to floating-point numbers correct to decimal numbers mentioned in the brackets of the setprecison.

When the value given to the setprecision() exceeds the number of floating point digits in the original number then 0 is increases to floating-point digit to match the precision mentioned by the user

 See the following example.

Now we take the same example in C. In C we can round off the floating-point number by just writing  .0,.1,.2,.3 before formate specifier. So we have to write down %.0f ,%.1f to round off up to nearest integer and one decimal place respectively  



See the following example.









Comments

Popular posts from this blog

Chandrayaan : A glorious legacy

Voyager : Journey of the twin to interstellar space

ISRO: From bicycle to a billion dreams