Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Basic input question

variable type

  • Please log in to reply
6 replies to this topic

#1 bgjyd834

bgjyd834

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 26 April 2011 - 11:16 PM

I'm trying to write a simple math program which computes the value of a trigonometric function. Basically just input one value and get another out, but I have two problems:
1. even with cmath included, the constant pi is not provided(I tried PI)
2. I can't seem to input values in the form of division ie 1/2, pi/3. I declared the input variable type double so does that mean you can't enter fractions during cin?

btw, I'm using Dev C++ V4.9.9.2

Thanks in advance
  • 0

#2 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 27 April 2011 - 11:34 AM

Sure you can use fractions when declared as double using cin. But fractions can be input as for e.g. 3.232. cin has no way of understanding if you put 3/2 or the like as input. There is no such basic type which is capable of taking rational numbers and computing the floating point result.

There is no such macro as pi or PI in c. you can use a float variable and assign 22/7 to it.

I will emphasize, writing

double b = 22/7;

is okay and legal.

but using cin with a float variable and passing 22/7 on command line expecting it would save it's fractional value is not
  • 0

#3 Megak

Megak

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 27 April 2011 - 12:37 PM

Sure you can use fractions when declared as double using cin. But fractions can be input as for e.g. 3.232. cin has no way of understanding if you put 3/2 or the like as input. There is no such basic type which is capable of taking rational numbers and computing the floating point result.

There is no such macro as pi or PI in c. you can use a float variable and assign 22/7 to it.

I will emphasize, writing

double b = 22/7;

is okay and legal.

but using cin with a float variable and passing 22/7 on command line expecting it would save it's fractional value is not


I believe pi exists in cmath, I've used it before. You could always add it in if you wish
  • 0

#4 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 27 April 2011 - 12:54 PM

I believe pi exists in cmath, I've used it before. You could always add it in if you wish


Which compiler were you using? GCC?

PI exists in GCC and some other variants but is not portable and not part of standard c/c++.
cmath (math.h) - C++ Reference

math.h - Wikipedia, the free encyclopedia
lists it under non standard and uses M_PI as macro.

So i believe it probably is not available with dev shed though it can be looked into math.h to be sure or try other options such as M_PI.
  • 0

#5 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 27 April 2011 - 01:01 PM

May be defining this could help _USE_MATH_DEFINES before including math.h as suggested at this stack over flow thread
trigonometry - How to use the PI constant in C++ - Stack Overflow
  • 0

#6 Megak

Megak

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 27 April 2011 - 02:38 PM

Which compiler were you using? GCC?

PI exists in GCC and some other variants but is not portable and not part of standard c/c++.
cmath (math.h) - C++ Reference

math.h - Wikipedia, the free encyclopedia
lists it under non standard and uses M_PI as macro.

So i believe it probably is not available with dev shed though it can be looked into math.h to be sure or try other options such as M_PI.


MinGW, so yes, GCC. I don't see why you would use anything else though. (Unless it's 16 bit or you need inline ASM)
  • 0

#7 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 27 April 2011 - 11:49 PM

There are a lot of tool chains and compilers which implement their proprietary extensions even in the 32 bit world.

The original thread was about devshed c++ compiler which does not have pi straight out of math.h at least
On windows, Microsoft Visual studio does not have pi defined either.
Borland for sure has a 32bit windows compiler which has a lot of non standard aspects such as it's UI.

I believe none of above is 16 bit or assembly only.

Moreover, there is only desktop world. If we go a little beyond into embedded, there are far more tool chains considering the variety of platforms.

ARM, MIPS, PowerPC, Xilinx are just families of architectures. Even each of them has many different tool chains.

In all of above we can be sure if we use something which is part of standard (ANSI) C or (ISO) C++ , it would be there as it is. But for non standard apis or macros, it may or may not be true. That is why when ever the term 'portable code' is used it is restricted to standard language specification only.

Hope that helps.
  • 0





Also tagged with one or more of these keywords: variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download