473,836 Members | 1,472 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 5462
In <dl************ *******@nwrddc0 1.gnilink.net> "P.J. Plauger" <pj*@dinkumware .com> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:c1******* ***@sunnews.cer n.ch...
In <AY************ ********@comcas t.com> "Douglas A. Gwyn"

<DA****@null.n et> 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.


Actually, scanf is quite good and well designed. I have only two
complaints about it:

1. Undefined behaviour on numeric input overflow. Making the conversion
fail would have been a lot more useful.

2. No escape sequence for specifying a null character in a scanset (\0
would terminate the format string).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #41
In <40************ **@News.individ ual.net> id**@hotmail.co m (Dave Hansen) writes:
On Thu, 26 Feb 2004 11:55:58 -0500, James Kuyper
<ku****@saicmo dis.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.


From octal dumps, that were quite popular on certain machines at the time
(most, if not all the DEC machines preceding the VAX had octal as their
"native" base). The PDP-11 is also a "natively" octal architecture: 8
general purpose registers and 8 addressing modes lead to the following
encoding for the two operand instructions: BOSSDD, where B was the byte
flag (0 for word instructions, 1 for byte instructions), O was the actual
opcode, SS was the register and addressing mode of the source operand,
DD was the register and addressing mode of the destination operand.

A byte in octal was written as a 3-digit number, just as it is written as
a 2-digit number in hex, even when one digit would be enough. So, when
one saw 012, he reacted as most of us would react today at 0A.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #42
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Apart from exponential notation for reals, I see no problem
building iron-clad numeric input parsers with one-char look
ahead.


Dealing with overflow without invoking undefined behaviour is the most
challenging part.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #43
On 26 Feb 2004 19:37:39 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <40************ **@News.individ ual.net> id**@hotmail.co m (Dave Hansen) writes:
On Thu, 26 Feb 2004 11:55:58 -0500, James Kuyper
<ku****@saicm odis.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
interprete d 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.


From octal dumps, that were quite popular on certain machines at the time

[...]
A byte in octal was written as a 3-digit number, just as it is written as
a 2-digit number in hex, even when one digit would be enough. So, when
one saw 012, he reacted as most of us would react today at 0A.


I learned and used octal several years before I dealt with hex much.
ISTR that most or all octal numbers on dumps began with zero. This is
why I said I thought Doug might be correct.

But I was hoping someone would say "PDP-1 MACRO Assembler [or some
other language translator] defined a similar syntax" as support for
the statement.

BCPL maybe?

Regards,

-=Dave
--
Change is inevitable, progress is not.
Nov 14 '05 #44
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
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.


Safety tip: Use 10 as the third (base) argument to strtoul, so 012
converts to 12. Don't use 0 as the base argument, since that's the
*only* form that lets the input prefix determine the base.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #45
Dan Pop wrote:
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?


What constitutes really good input checking and validation
is heavily application-dependent.
Nov 14 '05 #46
Keith Thompson wrote:
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.
While that is true in general, the practice harks back to
(at latest) responses to interactive prompts from DEC SYSGEN
programs, where the radix for input was under control of
the person entering the data, using 0 prefix for octal.
... 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.


Yes, that was the idea. A size, for example, might be
thought of in decimal or in some binary-related readix,
depending on how it was obtained.

In a similar vein, there are programs such as Unix "dd"
where sizes can be specified in bytes, "blocks", "Kbytes",
etc. through use of suffixes.

And of course as far back as early Fortran, numeric input
was allowed to optionally be expressed in a form of
"scientific notation".

Whether these are good ideas or not depends on many factors,
and is not something that the C standard is in a position to
determine.
Nov 14 '05 #47
In comp.std.c P.J. Plauger <pj*@dinkumware .com> wrote:

Safety tip: Use 10 as the third (base) argument to strtoul, so 012
converts to 12. Don't use 0 as the base argument, since that's the
*only* form that lets the input prefix determine the base.


But again, that's application dependent. For some applications, being
able to input octal or hex in addition to decimal is definitely a
feature, not a bug.

-Larry Jones

Oh, now YOU'RE going to start in on me TOO, huh? -- Calvin
Nov 14 '05 #48

"Roc" <no****@aqssys. com> wrote in message news:10******** *****@corp.supe rnews.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?


Most directly, from B. Not from BCPL. Richards's 1967 manual,
which we had, used an initial underlined 8 in its canonical syntax
to indicate an octal constant.
Someone once sent me the actual examples from this manual--
they're missing from my copy--but I've misplaced that reference.

In the BCPL compiler listings I have, it is initial $8 that signals
an octal constant; in the Richards & Whitby Strevens book
it is # (#X marks hexadecimal).

Dennis
Nov 14 '05 #49
"P.J. Plauger" <pj*@dinkumware .com> wrote in message
news:L3******** **********@nwrd dc01.gnilink.ne t...
"CBFalconer " <cb********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om...
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?


Because you might not have a guarantee that the current locale is "C"?

It seems reasonable that in other locales, strtoul() might recognize
additional spellings of the negative sign, or accept the sign after the
digits. How do I tell the difference between 2 and -4294967294 then?
Nov 14 '05 #50

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
7748
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
9668
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
10840
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
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.