Sponsored Content
Full Discussion: Division of int by double
Top Forums Programming Division of int by double Post 302911715 by Don Cragun on Saturday 2nd of August 2014 03:54:33 PM
Old 08-02-2014
With the code:
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
	int	t	= 1680;
	float	adj	= 1.12;
	int	ires	= (int)(t / adj);
	float	fres	= t / adj;
	int	rires	= (int)(t / adj + .5);

	printf("adj =\t%.20f\n", adj);
	printf("fres =\t%.20f\n", fres);
	printf("ires =\t%i\n", ires);
	printf("rires =\t%i\n", rires);
        return(0);
}

with or without the text shown in red, on Mac OS X 10.9.4, I get the output:
Code:
adj =	1.12000000476837158203
fres =	1500.00000000000000000000
ires =	1500
rires =	1500

which seems to match what you showed us from your Java code.

It is interesting that you're getting the line:
Code:
adj =   1.12000000476837158000

while I'm getting:
Code:
adj =	1.12000000476837158203

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:
 

10 More Discussions You Might Find Interesting

1. Programming

difference between int ** func() and int *& func()

What is the difference between int** func() and int*& func(). Can you please explain it with suitable example. Thanks, Devesh. (1 Reply)
Discussion started by: devesh
1 Replies

2. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

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)
Discussion started by: csnmgeek
2 Replies

3. UNIX for Dummies Questions & Answers

Problem in division

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)
Discussion started by: rochitsharma
9 Replies

4. Shell Programming and Scripting

division by 0 error

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)
Discussion started by: cat55
2 Replies

5. Shell Programming and Scripting

how to check for division by zero

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)
Discussion started by: npatwardhan
2 Replies

6. Shell Programming and Scripting

division by zero

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)
Discussion started by: 2retti
6 Replies

7. Programming

Handle int listen(int sockfd, int backlog) in TCP

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)
Discussion started by: sehang
3 Replies

8. UNIX for Advanced & Expert Users

awk: division by zero

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)
Discussion started by: kumar77
11 Replies

9. UNIX for Dummies Questions & Answers

Help in division

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)
Discussion started by: pandeesh
5 Replies

10. UNIX for Dummies Questions & Answers

Division of wc output

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)
Discussion started by: yatici
5 Replies
All times are GMT -4. The time now is 04:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy