Jump to content

Newbie question...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Ok so output I am getting is this:

100000, 2.600000, 25
259999

... when I use 100000, 2.6, and 25 as my inputs in this code...

#include <stdio.h>


main()

{

	char again;

	int  CurrentYr, count,i, NextYr;

	float Rate;

	

	do {

		printf("How many egrets currently exist?");

		scanf("%d",&CurrentYr);

		printf("At what rate is their population increasing (1-4)?");

		scanf("%f",&Rate);

		printf("How many years would you like to forcast?");

		scanf("%d",&count);


		printf("%d, %f, %d", CurrentYr, Rate, count);

		

		NextYr = CurrentYr * Rate; 

		

		printf("\n%d", NextYr);



		printf("\n\nWould you like to try again (y/n)");

		scanf("%c",&again);

	

	}while (scanf("%c",&again) != "n");

} 


Can someone please explain why 100000 * 2.6 = 259999??

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
2.6 is often stored as 2.59999999999999999999.
When multiplied,
100000 * 2.6 = 259999.999999999
Then storing it in an int results in truncation to 259999.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
How can I get around that?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
round - Math.h - C Reference with Worked Examples
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog