Is there a way to mask a decimal without rounding in ColdFusion?
Example:
45.5454
I want to get 45, not 46.
It depends on how you want to handle negative numbers.
If you want -45.5454 to be converted to -45, use Fix().
If you want -45.5454 to be converted to -46, use Int().
If you're only dealing with positive numbers either will suffice.
Fix
Description
Converts a real number to an integer.
Returns
If number is greater than or equal to 0, the closest integer less than or equal to number.
If number is less than 0, the closest integer greater than or equal to number.
myNumber=45.5454;
myResult=fix(myNumber);
Int
Description
Calculates the closest integer that is smaller than number. For example, it returns 3 for Int(3.3) and for Int(3.7); it returns -4 for Int(-3.3) and for Int(-3.7).
Returns
An integer, as a string.
myNumber=45.5454;
myResult=int(myNumber);
Use int:
#Int(5.2)# = 5
#Int(2.9)# = 2
Documentation
Related
I want to know why in formula to calculate range of any data type
i.e.2^(n-1),why it is n-1 ,n is the number of bits occupied by the given data type
Assuming that the type is unsigned, the maximum value is (2n)-1, because there are 2n values, and one of them is zero.
2(n-1) is the value of the n:th bit alone - bit 1 is 20, bit 2 is 21, and so on.
This is the same for any number base - in the decimal system, n digits can represent 10n different values, with the maximum value being 10n-1, and the n:th digit is "worth" 10(n-1).
For example, the largest number with three decimal digits is 999 (that is, 103-1), and the third decimal digit is "the hundreds digit", 102.
First 2^(n-1) is not correct, the maximum (unsigned) number represented by the data type is:
max = 2^n - 1
So for a 8 Bit data type, the maximum represented value is 255
2^n tells you the amount of numbers represented (256 for the 8-Bit example) but because you want to include 0 the range is 0 to 255 and not 1 to 256
I wanted to extract decimal places from decimal number. Not that hard, right?
So, I converted float to string.
Then, I used substr() to crop the string starting from string.find('.')+1, till the end.
Well.... the problem is here. When we have a number like this: 5.7741589, it's not "precise" that much, it's actually: 5.774159
If it round itself, I can't precisely get the number of decimal places... So question is:
How can I convert decimal number to string, without rounding it?
EDIT: Since a lot of people are asking for actual input and output and code, here it is:
Input: 5.7741589
Output (let's say we want to output just decimal places): 7741589
Output (not expected one): 774159
Code:
float num;
cin>>num;
string s = to_string(num);
s = s.substr(s.find('.')+1,s.length());
cout<<s<<endl;
EDIT2: One more thing. I need this for competitive programming, I can do this exercise with input as string. Imagine you have problem where you have 2 decimal numbers, you need to multiply them and then count number of decimal places. Then you again lose decimal places which is problem.
The rot sets in as soon as you use a binary floating point number to model a decimal number: a floating point scheme models a subset of the real numbers. In this respect it's no different to using an integral type: conceptually the issues that arise are no different.
If you want to model decimal numbers exactly then use a decimal type.
The result you are getting is because of rounding off decimal places. This is because as soon as you enter a floating point number, whatever may be the number of decimal places, it is stored with as many decimal places as the precision of the data type. So for float (which can store 8 decimal places), if you enter
1.1234 ---- Stored as ---> 1.12339997
1.123 ---- Stored as ---> 1.12300003
5.7741589 ---- Stored as ---> 5.77415895
So basically, you are storing the input in those number (8) of decimal places. What can be done is that you can use below snippet to forcefully use the desired number of digits after decimal places and convert it to string.
float num;
cin >> num;
char buf[20];
// 8 is as per the (precision required + 1). So in this case,
// we have 7 digits after decimal, so 8 is used.
sprintf(buf, "%.8f", num);
// Truncating the rounding off
buf[strlen(buf) - 1] = '\0';
string s(buf);
s = s.substr(s.find('.') + 1, s.length());
cout << s << endl;
Though with this approach, it would be necessary that all your inputs are having equal number of decimal places.
This is sort of a workaround, because the value 5.7741589 that you entered is not stored as it is. So, if the source itself is not what you entered, how can you get the desired output with the assumption that source is what you provided.
Let's see what happens (this answer supposes IEEE-754, with floats being 32-bit binary floating-point numbers).
First, you enter the decimal number as input: 5.7741589. This number has to be stored into a 32-bit binary floating point.
This number in binary is 101.11000110001011110100011100010101011010000100011...
To store this into a float, we need to round it (in binary). So cin>>num is lossy. It is rounded to the nearest 32-bit binary floating point number, which is:
in binary: 1.01110001100010111101001 * 2^2 = 101.110001100010111101001.
in decimal: 1.44353973865509033203125 * 2^2 = 5.774158954620361328125.
As you can see, there is no point talking about decimal places, after your input number is converted to float, because your input number has been modified (looking at your number in binary, it got rounded. Looking at the number as decimal, it got a lot of extra digits, its value slightly differs from the original one).
If you want to solve this problem, you need to input the number as string, or you need to use some kind of decimal floating-point library.
As the headline supposes I am trying to convert hex numbers like 0x320000dd to decimal numbers.
My code only works for positive numbers but fails when it comes to hex numbers that represent negative decimal numbers. Here is a excerpt of my code:
cin>>hex>> x;
unsigned int number = x;
int r = number & 0xffffff;
My input is alreay in hex, and the computer converts it automatically in an integer. What I am trying to do is getting the operand of the hex number, so the last 24 bits.
Can you help me get my code working for negative values like :0x32fffdc9 or 0x32ffffff? Thank's a lot!
EDIT:
I would like my output to be :
0x32fffdc9 --> -567
or
0x32ffffff --> -1
so just the plain decimal values but instead it gives me 16776649 and 16777215 for the upper examples.
Negative integers are typically stored in 2's complement Meaning that if your most significant bit (MSB) is not set the number is not negative. This means that just as you need to unset the 8-MSBs of your number to clamp a 32-bit number to a 24-bit positive number, so you'll need to set the 8-MSBs of your number to clamp to a negative number:
const int32_t r = 0x800000 & number ? 0xFF000000 | number : number & 0xFFFFFF;
vector<bool> or bitset may be worth your consideration, as they would clarify the hexadecimal numbers to the range of bits to be set.
In python, i cannot divide 5 by 22. When I try this, it gives me zero-even when i use float?!!
>>> print float(5/22)
0.0
It's a problem with order of operations. What's happening is this:
* First python takes 5/22. Since 5 and 22 are integers, it returns an integer result, rounding down. The result is 0
* Next you're converting to a float. So float(0) results in 0.0
What you want to do is force one (or both) operands to floats before dividing. e.g.
print 5.0/22 (if you know the numbers absolutely)
print float(x)/22 (if you need to work with a variable integer x)
Right now you're casting the result of integer division (5/22) to float. 5/22 in integer division is 0, so you'll be getting 0 from that. You need to call float(5)/22.
And when I was reading the chapter about float points in C++ Primier Plus.
It gave an example as shown below:
#include <iostream>
int main(){
using namespace std;
float a = 2.34E+22f;
float b = a + 1.0f;
cout << "a =" << a <<endl;
cout << "b -a =" << b - a<< endl;
return 0;
}
And its result is:
a = 2.34e+22
b -a = 0
The explanation from the book is and I quote:
"The problem is that 2.34E+22 represents a number with 23 digits to the left of the
decimal. By adding 1, you are attempting to add 1 to the 23rd digit in that number. But
type float can represent only the first 6 or 7 digits in a number, so trying to change the
23rd digit has no effect on the value."
But I do not understand it. Could anybody help me to understand why b -a is 0 in a understandable way please?
The float type in C/C++ is stored in the standard 'single precision' format. The numbers are of the form
±m*2^e
where m is an integer between 223 and 224, and e is an integer. (I'm leaving out a lot of details that are not relevant here.)
So how does this explain what you are seeing?
First of all, the numbers in your code are always "rounded" to the nearest floating-point number. So the value 2.34e+22 is actually rounded to 10391687*251, which is 23399998850475413733376. This is the value of a.
Second, the result of a floating-point operation is always rounded to the nearest floating-point number. So, if you add 1 to a, the result is 23399998850475413733377, which is again rounded to the nearest floating-point number, which is still, of course, 23399998850475413733376. So b gets the same value as a. Since both numbers are equal, a - b == 0.
You can add numbers that are much larger than 1 to a and still get the same result. The reason is again the fact that the result is rounded, and the closest floating-point number will still be a when you add numbers up to at least 250, or about 1e+15.
b - a is 0 because b and a are equal.
When you add a too small number to a large number, it's as if you didn't add anything at all.
In this case, "too small" would be anything less than about 2.34e+15 i.e. 7 digits smaller.
The single precision floating point type float is like this(assuming IEEE-754)
The fraction part has only 23 bits, roughly less than 107. When you add a rather small number to 2.34E+22f, the precision of float limits the result's representaion, so b end up with unchanged value from a.
Both the existing answers are correct (from Mark Ransom and Yu Hao).
The very short explanation is that float values are not very precise. The values are rounded off after 6 or 7 decimal digits. For very large numbers, this imprecision means that small changes to the value get rounded away to nothing. Even + 1 or + 100 can be a "very small change" if it's being done to 1000000000.