Форум » C/C++ для начинающих (C/C++ for beginners) » how to find float decimal palce » Ответить

how to find float decimal palce

ramees: i like to compare two float value the decimal place are matching eg float values 0.02541 , 0.06541 . if i multiply one of this value like --> 0.02541 * 100000 = 2541 so this value have 4 decimal place how can i find this

Ответов - 12

ramees: i wrote this code . is this code good or bad is there any better way to do this #include <iostream> #include <sstream> using namespace std; int main () { float val =1.1006; val = val*10000; cout<<val<<endl; stringstream ss (stringstream::in | stringstream::out); ss << val; string test = ss.str(); cout << test <<endl; std::cout << "The size of str is " << test.size() << " bytes.\n"; return 0; }

Сыроежка: The code is compiled successfully but it is not clear what you are trying to achieve.

ramees: i am tying to compare two float value that have same decimal place like this --> 0.12451 value have 5 decimal value ,---> this 0.00214 value have 3 decimal . i like to know is there any other way to do this

Сыроежка: I have not understood what you mean saying that 0.00214 has 3 decimal. Maybe you will find function modf declared in header cmath useful for yourself. It allows to extract the fraction of a floating number. For example [pre2] #include <iostream> #include <cmath> int main () { double val = 1.1006; double ipart; double fpart = std::modf( val, &ipart ); std::cout << ipart << '\t' << fpart << std::endl; return 0; } [/pre2] The output is [pre2] 1 0.1006 [/pre2]

ramees: no this is not i needed . in my way you can see (above code) that i first multiply value like 0.00214 * 100000 = 214 i multiply to remove unwanted zero from value then i want to count how many value position like 0th , 10th, 100th, or 1000th in this case 214 is 100th so it is 3 . then i want to compare check to next value that is matching or not above code that i convert float to string and use .size() operator to find this value. i like to know any better way to do this

Сыроежка: Instead of std::stringstream you could use function std::to_string.

ramees:

ramees: std::stringstream & std::to_string are working correct . but when i input big precision value like float a = 3.141593254 its rounding value 3.14159 so it's not converting full float value to string i need 11 value precision and i can't use std::to_string becouse its c++ 11 fetures

Сыроежка: The problem is that float numbers of type float are outputed with 6 decimal digits while float numbers of type double are outputed with 15 decimal digits. Consider the following code [pre2] #include <iostream> #include <iomanip> #include <limits> int main() { float a = 3.141593254; double b = 3.141593254; std::cout << std::numeric_limits<float>::digits10 << std::endl; std::cout << "a = " << std::setprecision( std::numeric_limits<float>::digits10 ) << a << std::endl; std::cout << std::numeric_limits<double>::digits10 << std::endl; std::cout << "b = " << std::setprecision( std::numeric_limits<double>::digits10 ) << b << std::endl; return 0; } [/pre2] The output is [pre2] 6 a = 3.14159 15 b = 3.141593254 [/pre2] Take into account that in this statement [pre2] float a = 3.141593254; [/pre2] the float literal used as initializer has type double while variable a has type float. So there is a loss of precision.

ramees: i need to do this in float because my array data , i accessing like const float* rIn = in[rchan] + x; i believe this is 32 bit float array is there any other way of doing this i tried to convert float to string and float to char array both code working but it did not get 11 precision all i want to do is look in the float value a left to right a = 0.001590254; in this case 0 to 4 and remove all the zero until any other value found and count how many digits in there so in this --> (1590254) 1 to 4 is the 7 digits size i like to get this size

Сыроежка: data type float has a restriction on the number of decimal digits it can store. If you have an array of float numbers then it can not represent exactly values as 0.001590254.

ramees: so i need to find other way

полная версия страницы