Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
D Cookbook

You're reading from   D Cookbook Discover the advantages of programming in D with over 100 incredibly effective recipes with this book and ebook.

Arrow left icon
Product type Paperback
Published in May 2014
Publisher
ISBN-13 9781783287215
Length 362 pages
Edition Edition
Arrow right icon
Author (1):
Arrow left icon
Adam Ruppe Adam Ruppe
Author Profile Icon Adam Ruppe
Adam Ruppe
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Core Tasks FREE CHAPTER Phobos – The Standard Library Ranges Integration Resource Management Wrapped Types Correctness Checking Reflection Code Generation Multitasking D for Kernel Coding Web and GUI Programming Addendum Index

Using the exponentiation operator


D has a built-in exponentiation operator: the ^^ operator.

How to do it…

In order to use the exponentiation operator, we need to execute the following steps:

  1. Write an expression with the ^^ operator.

  2. Make one or both arguments floating point if you want a floating point result.

The code is as follows:

int a = 2^^3; // a == 8
float b = 2.0 ^^ 3.0; // b == 8.0
auto c = 2 * 2 ^^ 3; // c == 16

How it works…

The exponentiation operator is first subject to constant folding and is then rewritten into a call to the std.math.pow library function to perform the operation. The result follows the regular arithmetic type rules of the D language, so the int arguments yield an int result and the float arguments yield a float result.

The operator also follows the usual arithmetic order of operations, so it has higher precedence than multiplication, as seen in the third example line.

The choice of ^^ for the operator was driven by the desire: to look like ASCII math without being confusing in the context of the D programming language. The two other major competitors, ^ and **, were rejected because they conflict with existing operations inherited by C: ^ is the bitwise XOR operator in both C and D, while ** is currently parsed as multiplication of a dereferenced pointer. While the lexer could be changed to make it into a new operator, this could potentially lead to silent breakages when porting C code to D, which D tries to avoid. (D doesn't mind breaking C code, but it prefers it to be a compile error whenever possible instead of code that compiles the same then acts differently.)

Thus, ^^ was chosen, and it is visually similar to how exponentiation is often written in ASCII text (2^3 for example), without being in conflict with any existing C or D code.

Note

Why isn't ^^ used for Boolean XOR in the same way that || is Boolean OR and && is Boolean AND? It is because Boolean XOR already has an operator: !=. If you write out the truth tables for is-not-equal, you'll find they are an exact match for a hypothetical Boolean XOR!

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image