I know a string value, is converted to a numeric representation and that is what the computer sees.
How about values that are already numeric?
So if I have a value of integer 128 and I store it in a variable that should hold unsigned integer. Am I certain the actual value the computer sees is 0b10000000?
I mean will numeric value map directly to their binary values?
Related
I need to count the number of digits in an SPSS numeric variable, and assign it to a different variable.
I tried converting it to a string and counting the length of the string with char.length(), but this returns the defined length of the variable, rather than the length of the actual string in each line.
Any ideas how this can be done?
when canculating the length of your string variable, use ltrimor rtrim(depending on how you calculated your string - just to be sure you could use both) to get rid of spaces and count only digits:
compute Ndigits=char.length(ltrim(rtrim(YourString))).
you could also do away with the text variable altogether and just use this function:
compute Ndigits=trunc(lg10(YourNumber))+1.
Note that the length will depend on how the number was converted to a string, i.e., the number of decimals specified in the conversion. Also, the decimal point character will contribute to the length.
Also, if you are in Unicode mode, which has been the default for several years, you don't need to use char.rtrim. Strings are automatically rtrimmed in that mode.
I want to convert a double to a string and only display needed decimals.
So I cannot use
d := 123.4
s := Format('%.2f', [d]);
As it display as the result is 123.40 when I want 123.4.
Here is a table of samples and expected result
|Double|Result as string|
-------------------------
|5 |5 |
|5.1 |5.1 |
|5.12 |5.12 |
|5.123 |5.123 |
You can use the %g format string:
General: The argument must be a floating-point value. The value is converted to the shortest possible decimal string using fixed or
scientific format. The number of significant digits in the resulting
string is given by the precision specifier in the format string; a
default precision of 15 is assumed if no precision specifier is
present. Trailing zeros are removed from the resulting string, and a
decimal point appears only if necessary. The resulting string uses the
fixed-point format if the number of digits to the left of the decimal
point in the value is less than or equal to the specified precision,
and if the value is greater than or equal to 0.00001. Otherwise the
resulting string uses scientific format.
This is not as simple as you think. It all boils down to representability.
Let's consider a simple example of 0.1. That value is not exactly representable in double. This is because double is a binary representation rather than a decimal representation.
A double value is stored in the form s*2^e, where s and e are the significand and exponent respectively, both integers.
Back to 0.1. That value cannot be exactly represented as a binary floating point value. No combination of significand and exponent exist that represent it. Instead the closest representable value will be used:
0.10000 00000 00000 00555 11151 23125 78270 21181 58340 45410 15625
If this comes as a shock I suggest the following references:
Is floating point math broken?
http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
http://floating-point-gui.de/
So, what to do? An obvious option is to switch to a decimal rather than binary representation. In Delphi that typically means using the Currency type. Depending on your application that might be a good choice, or it might be a terrible choice. If you wish to perform scientific or engineering calculations efficiently, for instance, then a decimal type is not appropriate.
Another option would be to look at how Python handles this. The repr function is meant, where possible, to yield a string with the property that eval(repr(x)) == x. In older versions of Python repr produced very long strings of the form 1.1000000000000001 when in fact 1.1 would suffice. Python adopted an algorithm that finds the shortest decimal expression that represents the floating point value. You could adopt the same approach. The snag is that the algorithm is very complex.
My application has to do operation on Hexadecimal values.
For example,
If the input given by user is '0010F750', then my application will tell you the user which is the nearest value (from some set of pre defined values) and will give next value by adding '0000E500'.
How we can perform Hexa Decimal operations Find nearest, Add, Subtract from DELPHI?
Performing operations on hexadecimal values does not really mean anything. Numbers are numbers. Hexadecimal is merely a representation using base 16.
All you need to do is convert these hex strings to integers and you can use standard arithmetic operations.
function HexStrToInt(const str: string): Integer;
begin
Result := StrToInt('$' + str);
end;
Add and subtract using + and -. Use IntToHex to express values as their hex representations.
Your application does not and cannot "do operation on Hexadecimal values". Rather, it operates on binary values stored in chunks of data organized as bytes.
What the USER sees and what the PROGRAM works with are two completely unrelated things.
The number one (1) in binary is 00000001, in hex is 01, in decimal is 1, and in ASCII has the hexadecimal value of 31. Try printing the value of Ord('1').
You need to convert the external representation of your data, in Hex, to an internal representation as an Integer. That's what David was pointing to earlier.
Then you'd need to apply your "rounding" to the numeric value, then convert it back to a Hex string for the user to see.
Search around for examples that let you implement a simple calculator and you'll understand better.
Suppose I have a PS input file that contains 9-byte long data. The input data would be numbers that are necessarily left aligned. Hence, in case the number in the input file is smaller than 9 bytes, the number would be trailed by spaces.
When I read such a number into a 9(9) variable and DISPLAY the value the value displayed would be the number followed by the spaces (checked with SET HEX ON in the spool).
But instead when I MOVE the value from the 9(9) variable into an S9(9) COMP variable and then DISPLAY its value, the value displayed would be some random numeric value.
My question here is, how does COBOL interpret/convert the value for an S9(9) COMP variable in the above scenario?
It's a computer, the results you get are far from random.
What did you think would happen?
You are MOVEing something which is not numeric, but which you have defined as numeric, to a binary field. You will have a value which relates to your data, just not what you expect.
You have compiler option NUMPROC(NOPFD). If you had NUMPROC(PFD) you would have got an S0C7 abend,
You should find that all your trailing spaces get treated as zero, with NUMPROC(NOPFD).
A zoned-decimal, prior to a calculation, or, as in your case, a conversion to binary, is "packed". Which means all the zones are tossed away, the sign and final digit are reversed.
So, in the packed number you only get the numerics and the sign.
As long as all the numerics are 0-9 and the sign is A-F, no S0C7. Garbled results, but no S0C7.
If we consider your final two trailing spaces, X'4040'. You have NUMPROC(NOPFD) so the compiler will "fix" the sign for you (to an F in this case). Toss the zones (so the first 4 goes) swap the last byte (become X'04'), fix the sign (becomes X'0F') and converts that value to binary (which is successful). You have turned spaces into zeros.
If you used NUMPROC(PFD) the sign fixing would not occur, and the convert to binary (CVB) would give you a S0C7 abend.
I'm working on parsing with haskell, I want to parse a timestamp value expressed in such a way
946685561.618847
I have no problem to recognize (parse) it, but my problem is about the type of the result. I think of two situations:
Is there a fractional type in Haskell so that the result can be associated with the fractional value?
If this is not the case then how to store this value, since Int range from -229 to 229 - 1?
There are actually multiple fractional types--there is even a whole Fractional class.
The most commonly used is a Double, which is a double-precision floating point number. You can also use Float which is single precision.
Another alternative is to use the Rational type, which lets you store a number as a ratio of two Integers. (Coincidentally, Integer is an unbounded integral type. Int is the name for the bounded version.)
These types (Double, Float and Rational) are good for storing rational values. If you just want to store a large integral value, use Integer which is unbounded. (That is, it can store arbitrarily sized integers.)