5 Replies - 5222 Views - Last Post: 02 October 2005 - 12:13 PM Rate Topic: -----

#1 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 16
  • View blog
  • Posts: 615
  • Joined: 13-August 05

float/double/? Question

Post icon  Posted 01 October 2005 - 02:59 PM

How come
float out;
out = 1/4;
printf("%f", out);
doesn't display as .25? In my program, it outputs 0.000000. It does the same with double types too.

My assignment is to make a program that adds up (1/n)! - so that if the user inputs 4, it would add up like this: (1/4) + (1/3) + (1/2) + (1/1). That value will be assigned to 'out' and 'out' will be printed. I'm not allowed to do it with recursion.

It's a pretty simple concept, so I think I've got the basic shape down, but I don't know why it's not working. I added in a couple of extra printf(""); lines to try to figure out where the problem is - maybe they'll help you too.

Here's the total program:
// fracts.cpp : This program mimics the exercise program non-recursively.
// ----------

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "stdio.h"

float g(int in);

main()
{
	int in;
	float out;

	printf("This program mimics the program from the assignment, non-recursively.\n-------------\n\nInput: ");
	in = GetInteger();
	out = 0;
	out = g(in);
	printf("output: %f", out);
	getchar();
}
//The actual function.
float g(int in)
{
	int i;
	float out, temp;

	out = 0.0;
	temp = 0.0;
	printf("%f\n", out);
	for (i=in; i>0; i--)
	{
  temp = (1/i);
  printf("%f\t", temp);
  out = out + temp;
  printf("%f\n", out);
	}
	return (out);
}


Is This A Good Question/Topic? 0
  • +

Replies To: float/double/? Question

#2 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: float/double/? Question

Posted 01 October 2005 - 03:23 PM

Attach an f and decimal places to your number to designate them as type float, not type integer:

float out;
out = 1.0/4.0f;
printf("%f", out);
Was This Post Helpful? 0
  • +
  • -

#3 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 16
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: float/double/? Question

Post icon  Posted 01 October 2005 - 03:29 PM

Oh my gosh - thank you very much. I can't believe I didn't see that; I even did it later in the prog., but not at the top.... Sorry to ask such a dumb question.

'Preciate it!!

SP
Was This Post Helpful? 0
  • +
  • -

#4 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: float/double/? Question

Posted 01 October 2005 - 03:36 PM

No problem - not a dumb question, everyone makes simple mistakes :D (often they're the hardest ones to find).
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: float/double/? Question

Posted 01 October 2005 - 10:03 PM

Instead of placing the decimal places along with the 'f' character there's another way to do it using typecasting.
out = (float)1/4;


Was This Post Helpful? 0
  • +
  • -

#6 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 16
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: float/double/? Question

Posted 02 October 2005 - 12:13 PM

I haven't learned that yet.... Sorry. I've heard abou it and seen a few examples, but if I tried it on my own, I'd more than likely just screw it up....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1