Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
communitycreator

What is cbrt() in Swift?

harris910

The cbrt() function in Swift returns the cube root of a number.

Here is the mathematical representation of the cbrt() function:

Figure 1: Mathematical representation of the cbrt() function

Syntax

cbrt(number)

Parameter

cbrt() requires a number as a parameter.

Return value

cbrt() returns the cube root of the number sent as a parameter.

Code example

The following example shows how to use cbrt() in Swift.

import Swift
import Foundation
//Perfect cube root
print ("The value of cbrt(8.0) : ", cbrt(8.0));

print ("The value of cbrt(0.0) : ",cbrt(0.0));

print ("The value of cbrt(27.0) : ",cbrt(27.0));

//Non-perfect cube root
print ("The value of cbrt(4.4) : ",cbrt(4.4));

//Negative number
print ("The value of cbrt(-4.5) : ",cbrt(-4.5));

Explanation

  • Line 2: We add the Foundation header required for cbrt() function.
  • Lines 4–8: We calculate the cube root value of perfect cube numbers 8, 0, and 27 using cbrt().
  • Line 11: We calculate the cube root value of the non-perfect cube number 4.4 using cbrt().
  • Line 14: We calculate the cube root value of the negative number -4.5 using cbrt().

RELATED TAGS

swift
communitycreator
RELATED COURSES

View all Courses

Keep Exploring