If you print it to the maximum precision a double really gives you, you will see that it's not really 1500 -- it's 1499.999999999999773. The .999999999999773 is truncated down to 0 since floating point numbers "round down" when you convert to int.
Just add 0.5 to the number before converting to integer to make it round the way you expect.
To expand a little on what Corona68 said... Although 1.12 is a nice terminating decimal value, the corresponding binary number is a non-terminating sequence (just like 1/3 in decimal arithmetic is a non-terminating .33333...).
For IEEE Std 754 double precision floating point, you get 15-17 significant digits for individual values. As you perform multiplications and divisions, the founding errors add up. If you add or subtract small numbers to or from large numbers, the small numbers may disappear completely.
When you assign a double precision floating point value to an integer object, the result is truncated, not rounded; but you can do the rounding on your own. To see what happened, try this:
which produces:
These 2 Users Gave Thanks to Don Cragun For This Post:
I tried the same in Java, and to my surprise it behaves differently:
I run it like this
Just a note - I could not say int ires = i / d; as Java complains about loss of precision, so I needed to be explicit here.
I don't know a lot about Java, I hope somebody would explain the different behavior here.
If you take the C program I gave you before and change all of the "double"s to "float"s, you'll get the output:
instead of the earlier results shown when using type double.
Using double made the difference between the binary floating point representation of 1.12 and the exact decimal value affect the results. With different values, both float and double could give you results just above, just below, or an exact match to the the decimal evaluation (and when the float and double values are inexact, one could be above and the other could be below the exact value). Typecasting floating point value using (int) in both C and Java truncates. In C you can round using:
i = f + .5;
In Java you can round using:
It has been a while since I've written any Java code (and I haven't tested the following), but did you try something like:
Note that using .printf() and using .format() in Java and using printf() in C round values; they don't truncate like a conversion from a floating point type to an integral type.
Last edited by Don Cragun; 08-02-2014 at 01:21 AM..
Reason: Fix typos.
I did try your suggestion to experiment with float, but my results still show ires = 1499
See below:
Tried this on three systems I mentioned above.
Just out of curiosity - why C does not have decimal(n,m) fixed-point data type? I think if I had a fixed-point available here I won't see the problem I am having.
With the code:
with or without the text shown in red, on Mac OS X 10.9.4, I get the output:
which seems to match what you showed us from your Java code.
It is interesting that you're getting the line:
while I'm getting:
But, as I said before, as long as you're performing floating point arithmetic (float, double, or long double) where one of the operands or the result of a division does not have an exact representation may produce a result that is slightly higher or slightly lower than the exact decimal calculation would yield. Any time you are converting a floating point value to integer you need to take care to make adjustments suitable for the results you want given the limitations of floating point arithmetic. (Note that depending on what values you're expecting and the operations that have been performed, adding .5 before converting to int might or might not be appropriate.)
I would assume that C doesn't include a decimal data type because the hardware on which C was initially developed didn't include instructions to process binary coded decimal data (and a lot of hardware today doesn't either).
Have you looked for an open-source version of the dc utility source to see how it handles arbitrary precision arithmetic?
I don't have any experience with it, but have you looked at XBCD_Math?
This User Gave Thanks to Don Cragun For This Post:
I have a function that outputs 3 lines for each result and I want to know how many results there are.
so for example
function | wc -l
24
but I want to see the result 8.
so is there a easy way to divide the result? (5 Replies)
hi,
The below commands result only the whole number(not giving the decimal values).
pandeeswaran@ubuntu:~$ echo 1,2,3,4|sed 's/,/\//g'|bc
0
pandeeswaran@ubuntu:~$ echo 1000,2,3|sed 's/,/\//g'|bc
166
How to make it to return the decimal values?
Thanks (5 Replies)
I received error "awk: division by zero" while executing the following statement.
SunOS 5.10 Generic_142900-15 sun4us sparc FJSV,GPUZC-M
echo 8 | awk 'END {printf ("%d\n",NR/$1 + 0.5);}' file1.lst
awk: division by zero
Can someone provide solution?
Thanks
Please use code... (11 Replies)
Hi,
from the manual
listen(2): listen for connections on socket - Linux man page
It has a parameter called backlog and it limits the maximum length of queue of pending list.
If I set backlog to 128, is it means no more than 128 packets can be handled by server?
If I have three... (3 Replies)
Hello,
I am searching for a way to calculate for example 10/100 within a shellscript and the result should be 0.1 and not just 0.
Every alternative i tried just results 0
Thank you in advance
2retti (6 Replies)
i have a script that is doing the following:
awk 'BEGIN {FS=","} ; {printf("%.10f",($5 - $2)/(3 * $3))}' data > test
now some records in $3 contain zeroes. i don't want to remove those records. is it possible to check for division by zero and then write a "N/A" for that record in the o/p... (2 Replies)
Hi,
I am writing a script that among other things will be checking for various files on mount points. One of the conditions is that unless the server has failed over the df command will show root ( / ). If when checking the files the script comes across /, I want it to skip it, otherwise to... (2 Replies)
hi
I am having two variables namely a=7 & b=8. I have to subtract these two variables. I am using the command
c=`expr $a / $b`
When I check the value of c, it comes out to be zero.
Please help.
Regards
Rochit (9 Replies)
hello everybody!
I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fileDescriptor;
fileDescriptor =... (2 Replies)