Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is cos() in Dart?

harris910

The cos() function in Dart returns the cosine of a number. To be more specific, cos() returns the cosine of a number in radians.

The mathematical representation of the cos() function is shown below.

Mathematical representation of the cosine function
  • dart:math is required for this function.
  • The cos() function only works for right-angled triangles.

Syntax


double cos(double num)

Parameters

This function requires a number that represents an angle in radians as a parameter.

To convert degrees to radians, use the following formula:


radians = degrees * ( pi / 180 )

Return value

cos() returns the cosine of the number (in radians) that is sent as a parameter.

Code

import 'dart:convert';
import 'dart:math';
void main() {
    
    print("The value of cos(2.3): ${cos(2.3)}");
    print("The value of cos(-2.3): ${cos(-2.3)}");

    double result = cos(180.0 * (pi / 180.0));
    print("The value of cos(180.0 * (pi / 180.0)): ${result}");
}

Explanation

  • The first print() shows the value of positive numbers (cos(2.3)) in radians.

  • The second print() shows the value of negative numbers (cos(-2.3)) in radians.

  • The result variable of type double stores the converted degrees into radians and then applies cos() on that value.


    // degrees = 180.0
    // PI = 3.14159265

RELATED TAGS

dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring