473,836 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fibonacci number

How to generate fibonacci mubers in C ?
Nov 14 '05
62 5461
"Dan Pop" <Da*****@cern.c h> wrote in message
news:c1******** **@sunnews.cern .ch...
In <AY************ ********@comcas t.com> "Douglas A. Gwyn" <DA****@null.ne t> writes:
Really good input checking and validation requires much
more than any Standard C library function provides.


And this is a deficiency of the C standard that the committee doesn't
bother to fix. Why should each robust C application have to invent its
own wheel?


Reading and parsing input is effectively recognizing a mini language.
It's pretty hard to generalize the appropriate error recovery for
malformed input, and even harder to make an all purpose parser to
do the heavy lifting. scanf is one attempt at generalizing what
we learned form decades of reading formatted input. The standardized
form is pretty comprehensive and consistent, but nobody in X3J11
working on the first C Standard was a fan of it, or felt it was
adequate for serious programming. Equally, none of us knew how to
make a significantly more robust input parser.

So I guess each robust C application will have to continue to invent
its own wheel until someone puts forth a wheel worth standardizing.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #31
Roc

"Douglas A. Gwyn" <DA****@null.ne t> wrote in message
news:AY******** ************@co mcast.com...
Thad Smith wrote:
Another example: application programs which accept 012 as a user input
and convert to 10, not the intended 12.
I chalk both problems up to baggage that C carries around because it is
has a legacy.


And some of that legacy has origins that predate Unix and C.
One saw 012 = 10. and ASCIZ (null-terminated) strings in DEC
software well before C appeared.


Someone provide the history on how 012 was interpreted as 10, please?
Nov 14 '05 #32
In article <10************ *@corp.supernew s.com>, Roc <no****@aqssys. com> wrote:
Someone provide the history on how 012 was interpreted as 10, please?


Leading zero -> base 8.

-- Richard
--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.

FreeBSD rules!
Nov 14 '05 #33
Roc wrote:

"Douglas A. Gwyn" <DA****@null.ne t> wrote in message
news:AY******** ************@co mcast.com...

....
And some of that legacy has origins that predate Unix and C.
One saw 012 = 10. and ASCIZ (null-terminated) strings in DEC
software well before C appeared.


Someone provide the history on how 012 was interpreted as 10, please?


For pretty much as long as C has been in existence, integer constants
that start with 0, when the second character is neither 'x' nor 'X', are
interpreted as being in octal. Octal 12 = 8*1+2 = 10 decimal. See
section 6.4.4.1.
Nov 14 '05 #34
On Thu, 26 Feb 2004 11:55:58 -0500, James Kuyper
<ku****@saicmod is.com> wrote:
Roc wrote:

"Douglas A. Gwyn" <DA****@null.ne t> wrote in message
news:AY******** ************@co mcast.com...

...
> And some of that legacy has origins that predate Unix and C.
> One saw 012 = 10. and ASCIZ (null-terminated) strings in DEC
> software well before C appeared.


Someone provide the history on how 012 was interpreted as 10, please?


For pretty much as long as C has been in existence, integer constants
that start with 0, when the second character is neither 'x' nor 'X', are
interpreted as being in octal. Octal 12 = 8*1+2 = 10 decimal. See
section 6.4.4.1.


Doug's claim is that 012 = 10 was around "well before C appeared."
I'm not saying he's wrong (in fact, I think he's right), but I, too,
would be curious where "leading 0 means octal" cam e from, if not from
C.

Regards,

-=Dave
--
Change is inevitable, progress is not.
Nov 14 '05 #35
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
It is not quite that simple. You also have to scan off any
leading blanks, and possibly all leading white space.
char first;
sscanf(input, " %c", &first)

Will do the job for you. Rocket science, indeed.
And if "-2"
is a legal input field for strtoul, what about "--2"?


Are you reading impaired or what?

3 If the value of base is zero, the expected form of the subject
sequence is that of an integer constant as described in 6.4.4.1,
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
optionally preceded by a plus or minus sign, but not including
^^^^^^^^^^^^^^^ ^^^^^^^^=^^^^^^ ^^^^^^^^^^^^^
an integer suffix. If the value of base is between 2 and 36
(inclusive), the expected form of the subject sequence is a
sequence of letters and digits representing an integer with the
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
radix specified by base, optionally preceded by a plus or minus
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^=^^^^^^^^^^^ ^^^
sign, but not including an integer suffix.
^^^^
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #36
"P.J. Plauger" wrote:
"Dan Pop" <Da*****@cern.c h> wrote in message
In <AY************ ********@comcas t.com> "Douglas A. Gwyn"
Really good input checking and validation requires much
more than any Standard C library function provides.


And this is a deficiency of the C standard that the committee
doesn't bother to fix. Why should each robust C application
have to invent its own wheel?


Reading and parsing input is effectively recognizing a mini
language. It's pretty hard to generalize the appropriate error
recovery for malformed input, and even harder to make an all
purpose parser to do the heavy lifting. scanf is one attempt at
generalizing what we learned form decades of reading formatted
input. The standardized form is pretty comprehensive and
consistent, but nobody in X3J11 working on the first C Standard
was a fan of it, or felt it was adequate for serious
programming. Equally, none of us knew how to make a
significantly more robust input parser.

So I guess each robust C application will have to continue to
invent its own wheel until someone puts forth a wheel worth
standardizing.


However I had always thought, until recently, that strtoul was a
fundamental spoke for such a wheel. It seems that it isn't. I
have no problems with needing to design such routines, but I do
have problems with suddenly finding out that it is necessary.
Especially when the only change really needed is to ban the
leading sign.

Are there really programs out there that take advantage of this
provision? This would be something the standards body might
investigate, with a view to making a change.

Apart from exponential notation for reals, I see no problem
building iron-clad numeric input parsers with one-char look
ahead. For reals, the only means I see is to define a faulty
exponent entry as being considered zero (apart from multi-char
push back, which is a major change).

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #37
Roc wrote:
"Douglas A. Gwyn" <DA****@null.ne t> wrote in message
Thad Smith wrote:

Another example: application programs which accept 012 as a
user input and convert to 10, not the intended 12.
I chalk both problems up to baggage that C carries around
because it is has a legacy.


And some of that legacy has origins that predate Unix and C.
One saw 012 = 10. and ASCIZ (null-terminated) strings in DEC
software well before C appeared.


Someone provide the history on how 012 was interpreted as 10,
please?


It still is. The leading '0' signals an octal base.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #38
"CBFalconer " <cb********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om...
"P.J. Plauger" wrote:
"Dan Pop" <Da*****@cern.c h> wrote in message
In <AY************ ********@comcas t.com> "Douglas A. Gwyn"

> Really good input checking and validation requires much
> more than any Standard C library function provides.

And this is a deficiency of the C standard that the committee
doesn't bother to fix. Why should each robust C application
have to invent its own wheel?
Reading and parsing input is effectively recognizing a mini
language. It's pretty hard to generalize the appropriate error
recovery for malformed input, and even harder to make an all
purpose parser to do the heavy lifting. scanf is one attempt at
generalizing what we learned form decades of reading formatted
input. The standardized form is pretty comprehensive and
consistent, but nobody in X3J11 working on the first C Standard
was a fan of it, or felt it was adequate for serious
programming. Equally, none of us knew how to make a
significantly more robust input parser.

So I guess each robust C application will have to continue to
invent its own wheel until someone puts forth a wheel worth
standardizing.


However I had always thought, until recently, that strtoul was a
fundamental spoke for such a wheel.


It is.
It seems that it isn't.
It seems *to you* that it isn't. I kinda like it.
I
have no problems with needing to design such routines, but I do
have problems with suddenly finding out that it is necessary.
Especially when the only change really needed is to ban the
leading sign.
Or skip it yourself. Why is that so hard?

Look, even good old scanf does a prescan to build a candidate
field before calling the relevant strto* function. You too can
do so. strtoul is arguably more useful if it includes the ability
to process a minus sign, which is easily skipped, than if it lacked
this ability, which you would then have to supply. But in any case
it does what it does, and what it does has (IMO) a reasonable
rationale.
Are there really programs out there that take advantage of this
provision?
Yes.
This would be something the standards body might
investigate, with a view to making a change.
Not a prayer, after all these years.
Apart from exponential notation for reals, I see no problem
building iron-clad numeric input parsers with one-char look
ahead. For reals, the only means I see is to define a faulty
exponent entry as being considered zero (apart from multi-char
push back, which is a major change).


Deciding where to end the parse and what to report back are
only part of a complete breakfast. Recovering from faulty
input is almost always situation specific, and a nontrivial
part of the program structure/strategy.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #39
CBFalconer <cb********@yah oo.com> writes:
Roc wrote:
"Douglas A. Gwyn" <DA****@null.ne t> wrote in message
Thad Smith wrote:

> Another example: application programs which accept 012 as a
> user input and convert to 10, not the intended 12.
> I chalk both problems up to baggage that C carries around
> because it is has a legacy.

And some of that legacy has origins that predate Unix and C.
One saw 012 = 10. and ASCIZ (null-terminated) strings in DEC
software well before C appeared.


Someone provide the history on how 012 was interpreted as 10,
please?


It still is. The leading '0' signals an octal base.


It signals an octal base in a literal in C source code. It's not
obvious that user input should have the same syntax as program source
code.

For a program whose end users are likely to be non-programmers,
interpreting 012 as an octal number is counterintuitiv e. OTOH, if the
program is going to be used only by its author or by fellow
programmers, being able to use octal and hexadecimal input can be
quite handy.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
13131
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers, and when the numbers get too large it maxes out the int and I can't count any higher. I am trying to use extremely large numbers, I would like to use up to 10^50 or so. So my question is how do I do this? I'm just learning the language and I...
5
7747
by: Niks | last post by:
Can anybody explain me what is a "Fibonacci search"? even an URL will do. Thanks for reading.
4
4873
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or not? Or, another way to look at this is that: Would anyone of you think it is worth a while to find out if there is any location on earth with the set of longitude and latitude numbers that coincides with the Fibonacci series? As I see it, if...
14
4950
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b; std::cout << a/b << std::endl;
12
1915
by: Santosh Krisnan | last post by:
hello all, I fiddled with BASIC in the early 90s but left it at that. Now I am trying to learn C. I tried to solve an exercise in my book, but it failes to compile. Can anyone tell me what the error messages mean & what I should do? thanks.
13
3180
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
6
4979
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
7
2002
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
1
8842
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your program should prompt the user to enter a limit and indicate whether the last number in the sequence printed is either even or odd.
0
9813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9665
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10834
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6976
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5817
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.