Cody

Problem 42758. Convert float to base N

Solution 839573

Submitted on 28 Feb 2016 by LY Cao
  • Size: 30
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
x = [0, -1, 0.5, 2.25, 10.125, -10.5]; s = {'0', '-1', '0.1', '10.01', '1010.001', '-1010.1'}; for n=1:length(x); assert(strcmpi(s{n}, float2base(x(n), 2))); end

2   Pass
x = [0, 1, 0.5, 2.25, 10.125]; s = {'0', '1', '0.8', '2.4', 'a.2'}; for n=1:length(x); assert(strcmpi(s{n}, float2base(x(n), 16))); end

3   Pass
% only checking 6 digits after period x = 3.14; s = {'11.0010001111','3.10753412172','3.23d70a3d70a'}; bases = [2,8,16]; for n=1:length(bases) assert(strncmpi(s{n}, float2base(x, bases(n)), 8)); end

Suggested Problems

More from this Author2

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!