Trusted answers to developer questions

harris910

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

Here is the mathematical representation of the `cbrt()`

function:

cbrt(number)

`cbrt()`

requires a number as a parameter.

`cbrt()`

returns the cube root of the number sent as a parameter.

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));

- 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

CONTRIBUTOR

harris910

RELATED COURSES

View all Courses

Keep Exploring

Related Courses