📄 complex.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file fp.sgml dated Fri Jul 2 23:51:18 2004 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/fp/complex.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:00 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 14.11</title><link href="fpexcepts.html" rev=precedes><link href="mathsrc.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="fpexcepts.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="mathsrc.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 14.11</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's a good way to implement complex numbers in C?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It is straightforward to define a simple structureand some arithmetic functions to manipulate them.C99 supports <TT>complex</TT> as a standard type.<a href="fn80.html" rel=subdocument>[footnote]</a>Here is atinyexample,to give you a feel for it:<pre>typedef struct { double real; double imag; } complex;#define Real(c) (c).real#define Imag(c) (c).imagcomplex cpx_make(double real, double imag){ complex ret; ret.real = real; ret.imag = imag; return ret;}complex cpx_add(complex a, complex b){ return cpx_make(Real(a) + Real(b), Imag(a) + Imag(b));}</pre>You can use these routineswith code like<pre> complex a = cpx_make(1, 2); complex b = cpx_make(3, 4); complex c = cpx_add(a, b);</pre>or, even more simply,<pre> complex c = cpx_add(cpx_make(1, 2), cpx_make(3, 4));</pre></p><p>See alsoquestions <a href="../struct/firstclass.html">2.7</a>, <a href="../struct/anonstruct.html">2.10</a>, and <a href="mathsrc.html">14.12</a>.</p><p>References:C9X Sec. 6.1.2.5, Sec. 7.8<br></p><!-- aend --><p><hr><a href="fpexcepts.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="mathsrc.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/fp/complex.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:00 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -